Skip to main content
The array module is how you build and transform lists in Lion. Because every Lion expression is valid JSON, arrays play a dual role: they are both executable expressions and data. The array/make function lets you treat an array as data — collecting its arguments into a list — without triggering the evaluator’s call semantics.

Construction and access

array/make

Collects its arguments into an array. This is the canonical way to produce a literal list in a Lion program.
["array/make", 1, 2, 3]
Result: [1, 2, 3]

array/head

Returns the first element of an array. Throws when the array is empty.
["array/head", ["array/make", 1, 2, 3]]
Result: 1

array/tail

Returns all elements after the first. Throws when the array is empty.
["array/tail", ["array/make", 1, 2, 3]]
Result: [2, 3]

array/length

Returns the number of elements in an array.
["array/length", ["array/make", 1, 2, 3]]
Result: 3

Combining arrays

array/concat

Concatenates two arrays and returns the combined list.
["array/concat", ["array/make", 1, 2], ["array/make", 3, 4]]
Result: [1, 2, 3, 4]

array/includes?

Returns true when the array contains the given value. Uses Effect’s Equal.equals for comparison, which falls back to referential equality (===) for plain JavaScript values.
["array/includes?", ["array/make", 1, 2, 3], 2]
Result: true

Higher-order functions

All three higher-order functions accept a Lion lambda as their function argument, so they integrate with the Lion evaluator rather than requiring plain JavaScript functions.

array/map

Applies a function to every element and returns the transformed array.
["array/map",
  ["array/make", 1, 2, 3],
  ["lambda", ["x"], ["number/add", "x", 1]]]
Result: [2, 3, 4]

array/flat-map

Applies a function to every element and flattens the resulting arrays by one level.
["array/flat-map",
  ["array/make", 1, 2],
  ["lambda", ["x"], ["array/make", "x", "x"]]]
Result: [1, 1, 2, 2]

array/reduce

Folds a list into a single value. Arguments are: the array, the initial accumulator, and a two-argument function that receives the accumulator and the current element.
["array/reduce",
  ["array/make", 1, 2, 3],
  0,
  ["lambda", ["acc", "x"], ["number/add", "acc", "x"]]]
Result: 6
array/map, array/flat-map, and array/reduce run each callback through the Lion evaluator. This means you can use any Lion expression — including nested function calls and begin blocks — inside a lambda passed to these functions.
Combine array/reduce with array/concat to flatten nested structures, or use array/flat-map when each element maps to a variable-length list.