Comprehensions and spread
List comprehensions, map comprehensions, and the spread operator for building collections concisely.
Summary
List comprehensions: [expr for x in iterable if cond]. Map comprehensions: #{key: val for x in iterable if cond}. Both support tuple destructuring in the binding position and an optional if filter. The spread operator (...) works in array literals, map literals, and struct update expressions. Struct spread creates a new instance while overriding specific fields.
Canonical
let squares = [x * x for x in 0..5]
println(squares) -- [0, 1, 4, 9, 16]
let evens = [x for x in 0..10 if x % 2 == 0]
println(evens) -- [0, 2, 4, 6, 8]let sq = #{x: x * x for x in [1, 2, 3]}
println(sq) -- {1: 1, 2: 4, 3: 9}
-- with tuple destructuring
let m = #{k: v for (k, v) in #{"a": 1, "b": 2}.entries()}
-- with filter
let even_sq = #{x: x * x for x in [1, 2, 3, 4] if x % 2 == 0}
println(even_sq) -- {2: 4, 4: 16}-- array spread
let a = [1, 2, 3]
let b = [...a, 4, 5] -- [1, 2, 3, 4, 5]
-- map spread
let m = #{"a": 1}
let m2 = #{...m, "b": 2} -- {"a": 1, "b": 2}
-- struct spread (update syntax)
let p = Point { x: 10, y: 20 }
let p2 = Point { ...p, y: 30 }