Unpack
In Goblin, unpack splits a single value into an array. It is the inverse of Pack.
USage
What you write
digits | :unpack(12345)
digits | :unpack(12345)
What you get
[1, 2, 3, 4, 5]
chars | :unpack("goblin")
chars | :unpack("goblin")
["g", "o", "b", "l", "i", "n"]
Behavior By Type
Integer
Unpack splits a positive integer into an array of its digits.
:unpack(1234) /// → [1, 2, 3, 4] :unpack(9) /// → [9] :unpack(100) /// → [1, 0, 0]
:unpack(1234) /// → [1, 2, 3, 4] :unpack(9) /// → [9] :unpack(100) /// → [1, 0, 0]
Zero
Zero is a special case. It returns an array containing a single zero.
:unpack(0) /// → [0]
:unpack(0) /// → [0]
Negative integer
Negative integers are out of domain. Unpack returns nil.
:unpack(-5) /// → nil
:unpack(-5) /// → nil
String
Unpack splits a string into an array of single-character strings.
:unpack("goblin") /// → ["g", "o", "b", "l", "i", "n"] :unpack("hi") /// → ["h", "i"] :unpack("") /// → []
:unpack("goblin") /// → ["g", "o", "b", "l", "i", "n"] :unpack("hi") /// → ["h", "i"] :unpack("") /// → []
Anything else
Passing nil, a float, an object, or an array returns nil.
:unpack(nil) /// → nil :unpack([1, 2, 3]) /// → nil
:unpack(nil) /// → nil :unpack([1, 2, 3]) /// → nil
Common Uses
Inspecting digits of a number
n | 48271 digits | :unpack(n) say digits[0] /// → 4 say digits[-1] /// → 1
n | 48271 digits | :unpack(n) say digits[0] /// → 4 say digits[-1] /// → 1
Working with individual characters
word | "goblin" chars | :unpack(word) chars[0] |! :title(chars[0]) say :pack(chars) /// → "Goblin"
word | "goblin" chars | :unpack(word) chars[0] |! :title(chars[0]) say :pack(chars) /// → "Goblin"
Round-tripping with pack
original | 12345 digits | :unpack(original) restored | :pack(digits) /// original == restored → true
original | 12345 digits | :unpack(original) restored | :pack(digits) /// original == restored → true
Zero Is Its Own Digit
Unlike other single-digit integers, zero produces [0] rather than an empty array. This keeps behavior consistent — every non-negative integer unpacks into the array of digits you would write by hand.
:unpack(5) /// → [5] one digit :unpack(0) /// → [0] one digit :unpack(50) /// → [5, 0] two digits
:unpack(5) /// → [5] one digit :unpack(0) /// → [0] one digit :unpack(50) /// → [5, 0] two digits
The Inverse
Pack is the inverse of unpack. It collapses an array back into a single value.
:pack([1, 2, 3, 4, 5]) /// → 12345 :pack(["g", "o", "b", "l", "i", "n"]) /// → "goblin"
:pack([1, 2, 3, 4, 5]) /// → 12345 :pack(["g", "o", "b", "l", "i", "n"]) /// → "goblin"
How They Work Together
Pack and unpack are designed to round-trip cleanly on integers and strings.
/// integers :pack(:unpack(12345)) /// → 12345 /// strings :pack(:unpack("goblin")) /// → "goblin"
/// integers :pack(:unpack(12345)) /// → 12345 /// strings :pack(:unpack("goblin")) /// → "goblin"
This makes them useful for digit manipulation, character-level string operations, and anywhere you need to move between a single value and its constituent parts.