List operations. auto-imported.
Return the number of elements in a list.
list.len([1, 2, 3]) // => 3
Get the element at index i, or none if out of bounds.
list.get([10, 20, 30], 1) // => some(20)
Get the element at index i, or return a default value.
list.get_or([1, 2], 5, 0) // => 0
Return a new list with the element at index i replaced.
list.set([1, 2, 3], 1, 99) // => [1, 99, 3]
Return a new list with elements at indices i and j swapped.
list.swap([1, 2, 3], 0, 2) // => [3, 2, 1]
Sort a list in ascending order.
list.sort([3, 1, 2]) // => [1, 2, 3]
Reverse the order of elements.
list.reverse([1, 2, 3]) // => [3, 2, 1]
Check if a list contains an element.
list.contains([1, 2, 3], 2) // => true
Pair each element with its index.
list.enumerate(["a", "b"]) // => [(0, "a"), (1, "b")]
Combine two lists into a list of pairs.
list.zip([1, 2], ["a", "b"]) // => [(1, "a"), (2, "b")]
Flatten a list of lists into a single list.
list.flatten([[1, 2], [3]]) // => [1, 2, 3]
Take the first n elements.
list.take([1, 2, 3, 4], 2) // => [1, 2]
Drop the first n elements.
list.drop([1, 2, 3, 4], 2) // => [3, 4]
Remove duplicate elements, preserving first occurrence.
list.unique([1, 2, 1, 3]) // => [1, 2, 3]
Find the first index of an element, or none.
list.index_of([10, 20, 30], 20) // => some(1)
Get the last element, or none if empty.
list.last([1, 2, 3]) // => some(3)
Split a list into chunks of size n.
list.chunk([1, 2, 3, 4, 5], 2) // => [[1, 2], [3, 4], [5]]
Sum all integers in a list.
list.sum([1, 2, 3]) // => 6
Multiply all integers in a list.
list.product([2, 3, 4]) // => 24
Get the first element, or none if empty.
list.first([1, 2, 3]) // => some(1)
Check if a list is empty.
list.is_empty([]) // => true
Find the minimum element, or none if empty.
list.min([3, 1, 2]) // => some(1)
Find the maximum element, or none if empty.
list.max([3, 1, 2]) // => some(3)
Join a list of strings with a separator.
list.join(["a", "b", "c"], "-") // => "a-b-c"
Apply a function to each element, returning a new list.
[1, 2, 3].map(fn(x) => x * 2) // => [2, 4, 6]
Keep elements that satisfy a predicate.
[1, 2, 3, 4].filter(fn(x) => x > 2) // => [3, 4]
Find the first element matching a predicate.
[1, 2, 3].find(fn(x) => x > 1) // => some(2)
Check if any element satisfies a predicate.
[1, 2, 3].any(fn(x) => x > 2) // => true
Check if all elements satisfy a predicate.
[2, 4, 6].all(fn(x) => x % 2 == 0) // => true
Sort by a key-extraction function (not a comparator). f extracts the sort key from each element.
["bb", "a", "ccc"].sort_by((s) => string.len(s)) // => ["a", "bb", "ccc"]
Map each element to a list and flatten the results.
[1, 2].flat_map(fn(x) => [x, x * 10]) // => [1, 10, 2, 20]
Map and filter in one pass: keep only some values.
["1", "x", "3"].filter_map(fn(s) => int.parse(s)) // => [1, 3]
Take elements from the front while a predicate holds.
[1, 2, 3, 1].take_while(fn(x) => x < 3) // => [1, 2]
Drop elements from the front while a predicate holds.
[1, 2, 3, 1].drop_while(fn(x) => x < 3) // => [3, 1]
Count elements that satisfy a predicate.
[1, 2, 3, 4].count(fn(x) => x > 2) // => 2
Split a list into two: elements matching and not matching a predicate.
[1, 2, 3, 4].partition(fn(x) => x % 2 == 0) // => ([2, 4], [1, 3])
Reduce a list by combining elements pairwise. Returns none if empty.
[1, 2, 3].reduce(fn(a, b) => a + b) // => some(6)
Group elements by a key function into a map.
["hi", "hey", "bye"].group_by(fn(s) => string.char_at(s, 0))
Create a list of integers from start (inclusive) to end (exclusive).
list.range(1, 5) // => [1, 2, 3, 4]
Extract a sublist from start to end index.
list.slice([1, 2, 3, 4, 5], 1, 4) // => [2, 3, 4]
Insert an element at index i, shifting elements right.
list.insert([1, 3], 1, 2) // => [1, 2, 3]
Remove the element at index i.
list.remove_at([1, 2, 3], 1) // => [1, 3]
Find the first index where a predicate holds.
[10, 20, 30].find_index(fn(x) => x > 15) // => some(1)
Return a new list with the element at index i transformed by f.
list.update([1, 2, 3], 1, fn(x) => x * 10) // => [1, 20, 3]
Create a list with a value repeated n times.
list.repeat(0, 3) // => [0, 0, 0]
Like fold, but returns all intermediate accumulator values.
[1, 2, 3].scan(0, fn(acc, x) => acc + x) // => [1, 3, 6]
Insert a separator between each element.
list.intersperse([1, 2, 3], 0) // => [1, 0, 2, 0, 3]
Return sliding windows of size n.
list.windows([1, 2, 3, 4], 2) // => [[1, 2], [2, 3], [3, 4]]
Remove consecutive duplicates.
list.dedup([1, 1, 2, 2, 1]) // => [1, 2, 1]
Combine two lists element-wise using a function.
list.zip_with([1, 2], [10, 20], fn(a, b) => a + b) // => [11, 22]
Reduce a list from left with an initial accumulator.
[1, 2, 3].fold(0, fn(acc, x) => acc + x) // => 6
Take the last N elements.
list.take_end([1, 2, 3, 4], 2) // => [3, 4]
Drop the last N elements.
list.drop_end([1, 2, 3, 4], 2) // => [1, 2]
Remove duplicates by key function, preserving first occurrence.
list.unique_by(["aa", "ab", "ba"], (s) => string.get(s, 0))
Return a randomly shuffled copy of the list.
list.shuffle([1, 2, 3, 4])
Sliding window of size N over the list.
list.window([1, 2, 3, 4], 2) // => [[1, 2], [2, 3], [3, 4]]
Append an element in place. Requires var binding.
list.push(xs, 42)
Remove and discard the last element in place. Requires var binding.
list.pop(xs)
Remove all elements in place. Requires var binding.
list.clear(xs)