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