Atom

Rest bindings and missing entries

Keep the first value and collect the rest

Place a rest binding at the end of a positional destructuring pattern.

Atom
[first, ...rest] = [2, 3, 4]
print(first)
print(count(rest))
Output
2
2

first gets the first item. rest is an array with the other two items.

Positional destructuring can use comma-separated names or [first, second, ...rest]. A rest binding requires an array or hybrid dense part. In comma-separated destructuring the rest binding goes last. In the bracketed form, a rest binding collects the suffix starting at its position, and an ordinary name still reads its own positional index. Named destructuring selects fields by the names on the left. Destructuring does not require the source to have exactly as many entries as the pattern. Missing dictionary keys become null. Index-based destructuring can fail when the input index is out of bounds. Bindings are assigned successively after evaluating the source.

Atom
use collection

seed = {region = 'north', difficulty = 2}
settings = {...seed, difficulty = 3}
entities = [{name = 'scout', hp = 8}, {name = 'guard', hp = 12}]
state = {entities = entities, settings = settings}
state.entities[0].hp -= 2

[first, ...others] = state.entities
{region, difficulty} = state.settings
assert(first.hp == 6 and count(others) == 1)
assert(region == 'north' and difficulty == 3)

hybrid = {'a', [5] = 'later', label = null}
assert(hybrid[5] == 'later' and hybrid[4] == null)
assert(collection.contains(hybrid, 'label'))
assert(not collection.contains(hybrid, 'missing'))