ddonche/goblin-lang
0.46.24
1
0
docs reference
[[pack]]

Pack


In Goblin, pack collapses an array into a single value. What you get back depends on what is in the array.


Usage

What you write

digits | [1, 2, 3, 4, 5]
number | :pack(digits)
digits | [1, 2, 3, 4, 5]
number | :pack(digits)

What you get

12345

letters | ["g", "o", "b", "l", "i", "n"]
word | :pack(letters)
letters | ["g", "o", "b", "l", "i", "n"]
word | :pack(letters)

"goblin"

Pack looks at what is in the array and decides what to produce.


Behavior By Type

Array of single digits

If every element is an integer between 0 and 9, pack folds them into a single integer.

:pack([1, 2, 3])       /// → 123
:pack([0, 0, 7])       /// → 7
:pack([9, 9, 9, 9])    /// → 9999
:pack([1, 2, 3])       /// → 123
:pack([0, 0, 7])       /// → 7
:pack([9, 9, 9, 9])    /// → 9999

Array of strings or chars

If every element is a string or character, pack concatenates them into a single string.

:pack(["g", "o", "b"])           /// → "goblin" (with more chars)
:pack(["hello", " ", "world"])   /// → "hello world"
:pack(["g", "o", "b"])           /// → "goblin" (with more chars)
:pack(["hello", " ", "world"])   /// → "hello world"

Mixed array

If the array contains a mix of strings, chars, integers, floats, or bools, pack stringifies everything and concatenates.

:pack([1, "gob", true])    /// → "1gobtrue"
:pack([3, ".", 1, "4"])    /// → "3.14"
:pack([1, "gob", true])    /// → "1gobtrue"
:pack([3, ".", 1, "4"])    /// → "3.14"

Empty array

An empty array packs to 0.

:pack([])   /// → 0
:pack([])   /// → 0

Scalar pass-through

If you pass a scalar directly instead of an array, pack returns it unchanged.

:pack("goblin")   /// → "goblin"
:pack(42)         /// → 42
:pack("goblin")   /// → "goblin"
:pack(42)         /// → 42

Returns nil

Pack returns nil if the array contains nil, objects, or nested arrays.

:pack([nil, 1, 2])          /// → nil
:pack([[1, 2], [3, 4]])     /// → nil
:pack([nil, 1, 2])          /// → nil
:pack([[1, 2], [3, 4]])     /// → nil


Common Uses

Building a number from digits

digits | secure_pick 6 from 0...9 with dups
pin    | :pack(digits)
digits | secure_pick 6 from 0...9 with dups
pin    | :pack(digits)

Picks 6 random digits and collapses them into a single number.

Building a string from characters

letters | secure_pick 8 from "a"..."z" with dups
word    | :pack(letters)
letters | secure_pick 8 from "a"..."z" with dups
word    | :pack(letters)

Picks 8 random letters and collapses them into a single string.

Building a password component

nums_arr | secure_pick 4 from 0...9 with dups
numbers  | :pack(nums_arr).str
nums_arr | secure_pick 4 from 0...9 with dups
numbers  | :pack(nums_arr).str

Packs digits into a number, then casts to string for concatenation.


Overflow

If the digit array is large enough that the resulting integer would overflow, pack returns nil.

huge | collect 100 of 9
:pack(huge)   /// → nil
huge | collect 100 of 9
:pack(huge)   /// → nil


The Inverse

Unpack is the inverse of pack. It splits a single value back into an array.

:unpack(12345)     /// → [1, 2, 3, 4, 5]
:unpack("goblin")  /// → ["g", "o", "b", "l", "i", "n"]
:unpack(12345)     /// → [1, 2, 3, 4, 5]
:unpack("goblin")  /// → ["g", "o", "b", "l", "i", "n"]


How It Differs From Collect

Pack and Collect solve different problems.

Pack collapses values that already exist into one thing. Collect generates new values by running something repeatedly.

/// pack — you have the pieces, you want one thing
word | :pack(["g", "o", "b", "l", "i", "n"])

/// collect — you want to generate the pieces first
rolls | collect 6 of roll 1d6
/// pack — you have the pieces, you want one thing
word | :pack(["g", "o", "b", "l", "i", "n"])

/// collect — you want to generate the pieces first
rolls | collect 6 of roll 1d6

Rule of Thumb

If you already have the values — pack. If you need to generate the values — collect.