List
Functions over List<T>, ry’s ordered sequence of values of one type. [1, 2, 3] builds a
list, and List.from_r converts an R vector or list. A function whose result items come from a
callback, such as List.map, requires them to be ry values (the
item constraint), so List.map([1, 2], \(x) list(x)) is
a type error.
@type List<T>| Function | Summary |
|---|---|
List.from_r |
Converts an R vector or list into a List, checking every item: it refuses NA, a classed object and a ragged list, and drops names. |
List.to_r |
Converts a List into the R vector it shares a representation with. |
List.len |
The number of elements. |
List.is_empty |
Whether the list has no elements. |
List.get |
The element at a 1-based position, or :Err(:OutOfBounds). |
List.set |
Replaces the element at a position; a position out of range leaves the list unchanged. |
List.update |
Replaces the element at a position with f of it. |
List.append |
Adds an element at the end. |
List.prepend |
Adds an element at the start. |
List.concat |
The elements of first followed by those of second. |
List.single |
A list of one element. |
List.repeat |
A list of count copies of value. |
List.range |
The integers from start up to and including end, or an empty list when end is smaller. |
List.reverse |
The elements in reverse order. |
List.join |
Concatenates a list of lists. |
List.map |
Applies f to every element. |
List.map_with_index |
Applies f to every element and its 1-based position. |
List.map2 |
Applies f to the elements of two lists pairwise, stopping at the shorter one. |
List.map3 |
Applies f to the elements at each position of three lists, up to the shortest one. |
List.map4 |
Applies f to the elements at each position of four lists, up to the shortest one. |
List.join_map |
Applies f, which returns a list, to every element and concatenates the results. |
List.keep_if |
The elements for which predicate is TRUE. |
List.keep_if_try |
The elements for which predicate returns :Ok(TRUE), or the first :Err it returns. |
List.drop_if |
The elements for which predicate is FALSE. |
List.keep_oks |
Applies f to every element and keeps the :Ok values. |
List.keep_errs |
Applies f to every element and keeps the :Err values. |
List.map_try |
Applies f to every element and stops at the first :Err. |
List.walk |
Folds from the first element: f gets the state and an element and returns the next state. |
List.walk_backwards |
Folds from the last element. |
List.walk_until |
Folds until f returns :Break(state); :Continue(state) goes on. |
List.walk_backwards_until |
Folds the list from the last element until f returns :Break(state). |
List.walk_with_index |
Folds the list, passing f each element’s 1-based position too. |
List.walk_with_index_until |
Folds the list with positions until f returns :Break(state). |
List.walk_from |
Folds the list from a 1-based position onwards. |
List.walk_from_until |
Folds the list from a position until f returns :Break(state). |
List.walk_try |
Folds the list while f returns :Ok(state), stopping at the first :Err. |
List.for_each |
Calls f on each element for its effect, and returns NULL invisibly. |
List.for_each_try |
Calls f on each element, stopping at the first :Err. |
List.any |
Whether predicate is TRUE for some element. |
List.all |
Whether predicate is TRUE for every element; TRUE for an empty list. |
List.contains |
Whether some element is identical() to value. |
List.count_if |
The number of elements for which predicate is TRUE. |
List.find_first |
The first element for which predicate is TRUE, or :Err(:NotFound). |
List.find_last |
The last element for which predicate is TRUE, or :Err(:NotFound). |
List.find_first_index |
The position of the first match, or :Err(:NotFound). |
List.find_last_index |
The position of the last match, or :Err(:NotFound). |
List.first |
The first element, or :Err(:ListWasEmpty). |
List.last |
The last element, or :Err(:ListWasEmpty). |
List.take_first |
The first count elements, or all of them if there are fewer. |
List.take_last |
The last count elements. |
List.drop_first |
Everything but the first count elements. |
List.drop_last |
Everything but the last count elements. |
List.drop_at |
Removes the element at a position. |
List.sublist |
count elements starting at position start. |
List.split_at |
The first count elements as before and the rest as others. |
List.split_first |
Splits the list around the first element identical to delimiter. |
List.split_last |
Splits the list around the last element identical to delimiter. |
List.split_on |
The runs of elements between elements identical to delimiter. |
List.split_on_list |
The runs of elements between occurrences of the sublist delimiter. |
List.chunks_of |
Groups of size elements; the last group may be shorter. |
List.intersperse |
Puts separator between every two elements. |
List.sort_with |
A stable sort by compare, which returns :LT, :EQ or :GT. |
List.sort_asc |
Sorts numbers from smallest to largest. |
List.sort_desc |
Sorts numbers from largest to smallest. |
List.swap |
Exchanges the elements at two positions; an out-of-range position leaves the list unchanged. |
List.replace |
Replaces the element at a position, returning the new list and the element it replaced. |
List.append_if_ok |
Adds an :Ok value at the end; an :Err leaves the list unchanged. |
List.prepend_if_ok |
Adds an :Ok value at the start; an :Err leaves the list unchanged. |
List.concat_utf8 |
Appends the UTF-8 bytes of a string to a list of bytes. |
List.with_capacity |
An empty list with room for capacity elements, so appending that many never copies. |
List.reserve |
The list with room for spare more elements. |
List.release_excess_capacity |
The list with no room beyond its elements, which gives back what List.reserve or List.with_capacity set aside. |
List.sum |
The sum of the elements; 0L for an empty list. |
List.product |
The product of the elements; 1L for an empty list. |
List.min |
The smallest element, or :Err(:ListWasEmpty). |
List.max |
The largest element, or :Err(:ListWasEmpty). |
List.starts_with |
Whether the list begins with the elements of prefix. |
List.ends_with |
Whether the list ends with the elements of suffix. |
List.from_r
Section titled “List.from_r”List.from_r <- function(vector: logical[]) -> List<Bool>List.from_r <- function(vector: integer[]) -> List<I64>List.from_r <- function(vector: double[]) -> List<F64>List.from_r <- function(vector: character[]) -> List<Str>List.from_r <- function(vector: raw[]) -> List<I64>List.from_r <- function<T: item>(vector: list[T]) -> List<T>#: fn(vector: logical[]) -> List<Bool>List.from_r <- function(vector)
#: fn(vector: integer[]) -> List<I64>List.from_r <- function(vector)
#: fn(vector: double[]) -> List<F64>List.from_r <- function(vector)
#: fn(vector: character[]) -> List<Str>List.from_r <- function(vector)
#: fn(vector: raw[]) -> List<I64>List.from_r <- function(vector)
#: <T: item> fn(vector: list[T]) -> List<T>List.from_r <- function(vector)Converts an R vector or list into a List, checking every item: it refuses NA, a classed
object and a ragged list, and drops names. A double vector becomes a List<F64>, an
integer or raw vector a List<I64>, a logical vector a List<Bool> and a character
vector a List<Str>.
List.from_r(c(3, 1, 2))#> [3, 1, 2]List.to_r
Section titled “List.to_r”List.to_r <- function(list: List<F64>) -> double[]List.to_r <- function(list: List<Bool>) -> logical[]List.to_r <- function(list: List<Str>) -> character[]List.to_r <- function(list: List<I64>) -> integer[] | double[]List.to_r <- function<T: item>(list: List<T>) -> list[T]#: fn(list: List<F64>) -> double[]List.to_r <- function(list)
#: fn(list: List<Bool>) -> logical[]List.to_r <- function(list)
#: fn(list: List<Str>) -> character[]List.to_r <- function(list)
#: fn(list: List<I64>) -> integer[] | double[]List.to_r <- function(list)
#: <T: item> fn(list: List<T>) -> list[T]List.to_r <- function(list)Converts a List into the R vector it shares a representation with. A List<I64>
becomes an integer vector when every item fits in 32 bits and a double vector otherwise. A
List<F64>, List<Bool> or List<Str> already passes to R code as a vector without this call.
List.to_r([3i64, 1i64])#> c(3L, 1L)List.len
Section titled “List.len”List.len <- function<T>(list: List<T>) -> I64#: <T> fn(list: List<T>) -> I64List.len <- function(list)The number of elements.
List.len(["a", "b"])#> 2i64List.is_empty
Section titled “List.is_empty”List.is_empty <- function<T>(list: List<T>) -> Bool#: <T> fn(list: List<T>) -> BoolList.is_empty <- function(list)Whether the list has no elements.
List.is_empty([])#> TRUEList.get
Section titled “List.get”List.get <- function<T>(list: List<T>, index: I64) -> Result<T, :OutOfBounds>#: <T> fn(list: List<T>, index: I64) -> Result<T, :OutOfBounds>List.get <- function(list, index)The element at a 1-based position, or :Err(:OutOfBounds).
List.get(["a", "b"], 3)#> :Err(:OutOfBounds)List.set
Section titled “List.set”List.set <- function<T>(list: List<T>, index: I64, value: T) -> List<T>#: <T> fn(list: List<T>, index: I64, value: T) -> List<T>List.set <- function(list, index, value)Replaces the element at a position; a position out of range leaves the list unchanged.
List.set(["a", "b"], 2, "z")#> ["a", "z"]List.update
Section titled “List.update”List.update <- function<T>(list: List<T>, index: I64, f: fn(T) -> T) -> List<T>#: <T> fn(list: List<T>, index: I64, f: fn(T) -> T) -> List<T>List.update <- function(list, index, f)Replaces the element at a position with f of it.
List.update([1, 2], 1, function(x) x + 10)#> [11i64, 2i64]List.append
Section titled “List.append”List.append <- function<T>(list: List<T>, value: T) -> List<T>#: <T> fn(list: List<T>, value: T) -> List<T>List.append <- function(list, value)Adds an element at the end.
List.append([1, 2], 3)#> [1i64, 2i64, 3i64]List.prepend
Section titled “List.prepend”List.prepend <- function<T>(list: List<T>, value: T) -> List<T>#: <T> fn(list: List<T>, value: T) -> List<T>List.prepend <- function(list, value)Adds an element at the start.
List.prepend([1, 2], 0)#> [0i64, 1i64, 2i64]List.concat
Section titled “List.concat”List.concat <- function<T>(first: List<T>, second: List<T>) -> List<T>#: <T> fn(first: List<T>, second: List<T>) -> List<T>List.concat <- function(first, second)The elements of first followed by those of second.
List.concat([1], [2, 3])#> [1i64, 2i64, 3i64]List.single
Section titled “List.single”List.single <- function<T>(value: T) -> List<T>#: <T> fn(value: T) -> List<T>List.single <- function(value)A list of one element.
List.single("only")#> ["only"]List.repeat
Section titled “List.repeat”List.repeat <- function<T>(value: T, count: I64) -> List<T>#: <T> fn(value: T, count: I64) -> List<T>List.repeat <- function(value, count)A list of count copies of value.
List.repeat("x", 3)#> ["x", "x", "x"]List.range
Section titled “List.range”List.range <- function(start: I64, end: I64) -> List<I64>#: fn(start: I64, end: I64) -> List<I64>List.range <- function(start, end)The integers from start up to and including end, or an empty list when end is smaller.
Unlike R’s :, it never counts down.
Both List.range and List.repeat stop with an error when the list would hold more than
2,147,483,647 elements, the longest vector an R integer index reaches.
List.range(2, 5)#> [2i64, 3i64, 4i64, 5i64]List.range(3, 1)#> []List.reverse
Section titled “List.reverse”List.reverse <- function<T>(list: List<T>) -> List<T>#: <T> fn(list: List<T>) -> List<T>List.reverse <- function(list)The elements in reverse order.
List.reverse([1, 2, 3])#> [3i64, 2i64, 1i64]List.join
Section titled “List.join”List.join <- function<T>(lists: List<List<T>>) -> List<T>#: <T> fn(lists: List<List<T>>) -> List<T>List.join <- function(lists)Concatenates a list of lists.
List.join([[1], [], [2, 3]])#> [1i64, 2i64, 3i64]List.map
Section titled “List.map”List.map <- function<A, B: item>(list: List<A>, f: fn(A) -> B) -> List<B>#: <A, B: item> fn(list: List<A>, f: fn(A) -> B) -> List<B>List.map <- function(list, f)Applies f to every element.
List.map([1, 2, 3], function(x) x * 2)#> [2i64, 4i64, 6i64]List.map_with_index
Section titled “List.map_with_index”List.map_with_index <- function<A, B: item>(list: List<A>, f: fn(A, I64) -> B) -> List<B>#: <A, B: item> fn(list: List<A>, f: fn(A, I64) -> B) -> List<B>List.map_with_index <- function(list, f)Applies f to every element and its 1-based position.
List.map_with_index(["a", "b"], function(x, i) Str.concat(x, Num.to_str(i)))#> ["a1", "b2"]List.map2
Section titled “List.map2”List.map2 <- function<A, B, C: item>(first: List<A>, second: List<B>, f: fn(A, B) -> C) -> List<C>#: <A, B, C: item> fn(first: List<A>, second: List<B>, f: fn(A, B) -> C) -> List<C>List.map2 <- function(first, second, f)Applies f to the elements of two lists pairwise, stopping at the shorter one.
List.map2([1, 2, 3], [10, 20], function(a, b) a + b)#> [11i64, 22i64]List.map3
Section titled “List.map3”List.map3 <- function<A, B, C, D: item>(first: List<A>, second: List<B>, third: List<C>, f: fn(A, B, C) -> D) -> List<D>#: <A, B, C, D: item> fn(first: List<A>, second: List<B>, third: List<C>, f: fn(A, B, C) -> D) -> List<D>List.map3 <- function(first, second, third, f)Applies f to the elements at each position of three lists, up to the shortest one.
List.map3([1, 2], [10, 20], [100, 200], function(a, b, c) a + b + c)#> [111i64, 222i64]List.map4
Section titled “List.map4”List.map4 <- function<A, B, C, D, E: item>(first: List<A>, second: List<B>, third: List<C>, fourth: List<D>, f: fn(A, B, C, D) -> E) -> List<E>#: <A, B, C, D, E: item> fn(first: List<A>, second: List<B>, third: List<C>, fourth: List<D>, f: fn(A, B, C, D) -> E) -> List<E>List.map4 <- function(first, second, third, fourth, f)Applies f to the elements at each position of four lists, up to the shortest one.
List.map4([1], [2], [3], [4], function(a, b, c, d) a * b * c * d)#> [24i64]List.join_map
Section titled “List.join_map”List.join_map <- function<A, B>(list: List<A>, f: fn(A) -> List<B>) -> List<B>#: <A, B> fn(list: List<A>, f: fn(A) -> List<B>) -> List<B>List.join_map <- function(list, f)Applies f, which returns a list, to every element and concatenates the results.
List.join_map([1, 2], function(x) [x, x])#> [1i64, 1i64, 2i64, 2i64]List.keep_if
Section titled “List.keep_if”List.keep_if <- function<T>(list: List<T>, predicate: fn(T) -> Bool) -> List<T>#: <T> fn(list: List<T>, predicate: fn(T) -> Bool) -> List<T>List.keep_if <- function(list, predicate)The elements for which predicate is TRUE.
List.keep_if([1, 5, 2], function(x) x > 1)#> [5i64, 2i64]List.keep_if_try
Section titled “List.keep_if_try”List.keep_if_try <- function<T, E>(list: List<T>, predicate: fn(T) -> Result<Bool, E>) -> Result<List<T>, E>#: <T, E> fn(list: List<T>, predicate: fn(T) -> Result<Bool, E>) -> Result<List<T>, E>List.keep_if_try <- function(list, predicate)The elements for which predicate returns :Ok(TRUE), or the first :Err it returns.
List.keep_if_try(["1", "20", "3"], function(s) Result.map(Str.to_i64(s), function(n) n > 2))#> :Ok(["20", "3"])List.keep_if_try(["1", "x"], function(s) Result.map(Str.to_i64(s), function(n) n > 2))#> :Err(:InvalidNumStr)List.drop_if
Section titled “List.drop_if”List.drop_if <- function<T>(list: List<T>, predicate: fn(T) -> Bool) -> List<T>#: <T> fn(list: List<T>, predicate: fn(T) -> Bool) -> List<T>List.drop_if <- function(list, predicate)The elements for which predicate is FALSE.
List.drop_if([1, 5, 2], function(x) x > 1)#> [1i64]List.keep_oks
Section titled “List.keep_oks”List.keep_oks <- function<A, B: item, E>(list: List<A>, f: fn(A) -> Result<B, E>) -> List<B>#: <A, B: item, E> fn(list: List<A>, f: fn(A) -> Result<B, E>) -> List<B>List.keep_oks <- function(list, f)Applies f to every element and keeps the :Ok values.
List.keep_oks(["1", "x", "3"], Str.to_i64)#> [1i64, 3i64]List.keep_errs
Section titled “List.keep_errs”List.keep_errs <- function<A, B, E: item>(list: List<A>, f: fn(A) -> Result<B, E>) -> List<E>#: <A, B, E: item> fn(list: List<A>, f: fn(A) -> Result<B, E>) -> List<E>List.keep_errs <- function(list, f)Applies f to every element and keeps the :Err values.
List.keep_errs(["1", "x"], Str.to_i64)#> [:InvalidNumStr]List.map_try
Section titled “List.map_try”List.map_try <- function<A, B: item, E>(list: List<A>, f: fn(A) -> Result<B, E>) -> Result<List<B>, E>#: <A, B: item, E> fn(list: List<A>, f: fn(A) -> Result<B, E>) -> Result<List<B>, E>List.map_try <- function(list, f)Applies f to every element and stops at the first :Err.
List.map_try(["1", "2"], Str.to_i64)#> :Ok([1i64, 2i64])List.walk
Section titled “List.walk”List.walk <- function<T, S>(list: List<T>, state: S, f: fn(S, T) -> S) -> S#: <T, S> fn(list: List<T>, state: S, f: fn(S, T) -> S) -> SList.walk <- function(list, state, f)Folds from the first element: f gets the state and an element and returns the next state.
List.walk([1, 2, 3], 0, function(total, x) total + x)#> 6i64List.walk_backwards
Section titled “List.walk_backwards”List.walk_backwards <- function<T, S>(list: List<T>, state: S, f: fn(S, T) -> S) -> S#: <T, S> fn(list: List<T>, state: S, f: fn(S, T) -> S) -> SList.walk_backwards <- function(list, state, f)Folds from the last element.
List.walk_backwards(["a", "b", "c"], "", function(text, x) Str.concat(text, x))#> "cba"List.walk_until
Section titled “List.walk_until”List.walk_until <- function<T, S>(list: List<T>, state: S, f: fn(S, T) -> :Continue(S) | :Break(S)) -> S#: <T, S> fn(list: List<T>, state: S, f: fn(S, T) -> :Continue(S) | :Break(S)) -> SList.walk_until <- function(list, state, f)Folds until f returns :Break(state); :Continue(state) goes on.
List.walk_until([1, 2, 3, 4], 0, function(total, x) if (total >= 3) :Break(total) else :Continue(total + x))#> 3i64List.walk_backwards_until
Section titled “List.walk_backwards_until”List.walk_backwards_until <- function<T, S>(list: List<T>, state: S, f: fn(S, T) -> :Continue(S) | :Break(S)) -> S#: <T, S> fn(list: List<T>, state: S, f: fn(S, T) -> :Continue(S) | :Break(S)) -> SList.walk_backwards_until <- function(list, state, f)Folds the list from the last element until f returns :Break(state).
List.walk_backwards_until([1, 5, 2, 3], 0, function(total, x) if (x > 4) :Break(total) else :Continue(total + x))#> 5i64List.walk_with_index
Section titled “List.walk_with_index”List.walk_with_index <- function<T, S>(list: List<T>, state: S, f: fn(S, T, I64) -> S) -> S#: <T, S> fn(list: List<T>, state: S, f: fn(S, T, I64) -> S) -> SList.walk_with_index <- function(list, state, f)Folds the list, passing f each element’s 1-based position too.
List.walk_with_index([10, 20, 30], 0, function(total, x, i) total + x * i)#> 140i64List.walk_with_index_until
Section titled “List.walk_with_index_until”List.walk_with_index_until <- function<T, S>(list: List<T>, state: S, f: fn(S, T, I64) -> :Continue(S) | :Break(S)) -> S#: <T, S> fn(list: List<T>, state: S, f: fn(S, T, I64) -> :Continue(S) | :Break(S)) -> SList.walk_with_index_until <- function(list, state, f)Folds the list with positions until f returns :Break(state).
List.walk_with_index_until(["a", "b", "c"], "", function(seen, x, i) if (i > 2) :Break(seen) else :Continue(Str.concat(seen, x)))#> "ab"List.walk_from
Section titled “List.walk_from”List.walk_from <- function<T, S>(list: List<T>, index: I64, state: S, f: fn(S, T) -> S) -> S#: <T, S> fn(list: List<T>, index: I64, state: S, f: fn(S, T) -> S) -> SList.walk_from <- function(list, index, state, f)Folds the list from a 1-based position onwards.
List.walk_from([1, 2, 3, 4], 3, 0, function(total, x) total + x)#> 7i64List.walk_from_until
Section titled “List.walk_from_until”List.walk_from_until <- function<T, S>(list: List<T>, index: I64, state: S, f: fn(S, T) -> :Continue(S) | :Break(S)) -> S#: <T, S> fn(list: List<T>, index: I64, state: S, f: fn(S, T) -> :Continue(S) | :Break(S)) -> SList.walk_from_until <- function(list, index, state, f)Folds the list from a position until f returns :Break(state).
List.walk_from_until([1, 2, 3, 4], 2, 0, function(total, x) if (total >= 5) :Break(total) else :Continue(total + x))#> 5i64List.walk_try
Section titled “List.walk_try”List.walk_try <- function<T, S, E>(list: List<T>, state: S, f: fn(S, T) -> Result<S, E>) -> Result<S, E>#: <T, S, E> fn(list: List<T>, state: S, f: fn(S, T) -> Result<S, E>) -> Result<S, E>List.walk_try <- function(list, state, f)Folds the list while f returns :Ok(state), stopping at the first :Err.
List.walk_try(["1", "2", "x"], 0, function(total, s) Result.map(Str.to_i64(s), function(n) total + n))#> :Err(:InvalidNumStr)List.for_each
Section titled “List.for_each”List.for_each <- function<T>(list: List<T>, f: fn(T) -> Any) -> NULL#: <T> fn(list: List<T>, f: fn(T) -> Any) -> NULLList.for_each <- function(list, f)Calls f on each element for its effect, and returns NULL invisibly.
List.for_each(["a", "b"], function(x) cat(x, "\n"))#> a#> bList.for_each_try
Section titled “List.for_each_try”List.for_each_try <- function<T, E>(list: List<T>, f: fn(T) -> Result<Any, E>) -> Result<NULL, E>#: <T, E> fn(list: List<T>, f: fn(T) -> Result<Any, E>) -> Result<NULL, E>List.for_each_try <- function(list, f)Calls f on each element, stopping at the first :Err.
List.for_each_try([1, -2, 3], function(x) if (x < 0) :Err(:Negative(x)) else :Ok(NULL))#> :Err(:Negative(-2i64))List.any
Section titled “List.any”List.any <- function<T>(list: List<T>, predicate: fn(T) -> Bool) -> Bool#: <T> fn(list: List<T>, predicate: fn(T) -> Bool) -> BoolList.any <- function(list, predicate)Whether predicate is TRUE for some element.
List.any([1, 5], function(x) x > 4)#> TRUEList.all
Section titled “List.all”List.all <- function<T>(list: List<T>, predicate: fn(T) -> Bool) -> Bool#: <T> fn(list: List<T>, predicate: fn(T) -> Bool) -> BoolList.all <- function(list, predicate)Whether predicate is TRUE for every element; TRUE for an empty list.
List.all([1, 5], function(x) x > 4)#> FALSEList.contains
Section titled “List.contains”List.contains <- function<T: eq>(list: List<T>, value: T) -> Bool#: <T: eq> fn(list: List<T>, value: T) -> BoolList.contains <- function(list, value)Whether some element is identical() to value.
List.contains(["a", "b"], "b")#> TRUEList.count_if
Section titled “List.count_if”List.count_if <- function<T>(list: List<T>, predicate: fn(T) -> Bool) -> I64#: <T> fn(list: List<T>, predicate: fn(T) -> Bool) -> I64List.count_if <- function(list, predicate)The number of elements for which predicate is TRUE.
List.count_if([1, 5, 7], function(x) x > 4)#> 2i64List.find_first
Section titled “List.find_first”List.find_first <- function<T>(list: List<T>, predicate: fn(T) -> Bool) -> Result<T, :NotFound>#: <T> fn(list: List<T>, predicate: fn(T) -> Bool) -> Result<T, :NotFound>List.find_first <- function(list, predicate)The first element for which predicate is TRUE, or :Err(:NotFound).
List.find_first([1, 5, 7], function(x) x > 4)#> :Ok(5i64)List.find_last
Section titled “List.find_last”List.find_last <- function<T>(list: List<T>, predicate: fn(T) -> Bool) -> Result<T, :NotFound>#: <T> fn(list: List<T>, predicate: fn(T) -> Bool) -> Result<T, :NotFound>List.find_last <- function(list, predicate)The last element for which predicate is TRUE, or :Err(:NotFound).
List.find_last([1, 5, 7], function(x) x > 4)#> :Ok(7i64)List.find_first_index
Section titled “List.find_first_index”List.find_first_index <- function<T>(list: List<T>, predicate: fn(T) -> Bool) -> Result<I64, :NotFound>#: <T> fn(list: List<T>, predicate: fn(T) -> Bool) -> Result<I64, :NotFound>List.find_first_index <- function(list, predicate)The position of the first match, or :Err(:NotFound).
List.find_first_index([1, 5, 7], function(x) x > 4)#> :Ok(2i64)List.find_last_index
Section titled “List.find_last_index”List.find_last_index <- function<T>(list: List<T>, predicate: fn(T) -> Bool) -> Result<I64, :NotFound>#: <T> fn(list: List<T>, predicate: fn(T) -> Bool) -> Result<I64, :NotFound>List.find_last_index <- function(list, predicate)The position of the last match, or :Err(:NotFound).
List.find_last_index([1, 5, 7], function(x) x > 9)#> :Err(:NotFound)List.first
Section titled “List.first”List.first <- function<T>(list: List<T>) -> Result<T, :ListWasEmpty>#: <T> fn(list: List<T>) -> Result<T, :ListWasEmpty>List.first <- function(list)The first element, or :Err(:ListWasEmpty).
List.first(["a", "b"])#> :Ok("a")List.last
Section titled “List.last”List.last <- function<T>(list: List<T>) -> Result<T, :ListWasEmpty>#: <T> fn(list: List<T>) -> Result<T, :ListWasEmpty>List.last <- function(list)The last element, or :Err(:ListWasEmpty).
List.last([])#> :Err(:ListWasEmpty)List.take_first
Section titled “List.take_first”List.take_first <- function<T>(list: List<T>, count: I64) -> List<T>#: <T> fn(list: List<T>, count: I64) -> List<T>List.take_first <- function(list, count)The first count elements, or all of them if there are fewer.
List.take_first([1, 2, 3], 2)#> [1i64, 2i64]List.take_last
Section titled “List.take_last”List.take_last <- function<T>(list: List<T>, count: I64) -> List<T>#: <T> fn(list: List<T>, count: I64) -> List<T>List.take_last <- function(list, count)The last count elements.
List.take_last([1, 2, 3], 2)#> [2i64, 3i64]List.drop_first
Section titled “List.drop_first”List.drop_first <- function<T>(list: List<T>, count: I64) -> List<T>#: <T> fn(list: List<T>, count: I64) -> List<T>List.drop_first <- function(list, count)Everything but the first count elements.
List.drop_first([1, 2, 3], 2)#> [3i64]List.drop_last
Section titled “List.drop_last”List.drop_last <- function<T>(list: List<T>, count: I64) -> List<T>#: <T> fn(list: List<T>, count: I64) -> List<T>List.drop_last <- function(list, count)Everything but the last count elements.
List.drop_last([1, 2, 3], 2)#> [1i64]List.drop_at
Section titled “List.drop_at”List.drop_at <- function<T>(list: List<T>, index: I64) -> List<T>#: <T> fn(list: List<T>, index: I64) -> List<T>List.drop_at <- function(list, index)Removes the element at a position.
List.drop_at(["a", "b", "c"], 2)#> ["a", "c"]List.sublist
Section titled “List.sublist”List.sublist <- function<T>(list: List<T>, start: I64, count: I64) -> List<T>#: <T> fn(list: List<T>, start: I64, count: I64) -> List<T>List.sublist <- function(list, start, count)count elements starting at position start.
List.sublist([1, 2, 3, 4], 2, 2)#> [2i64, 3i64]List.split_at
Section titled “List.split_at”List.split_at <- function<T>(list: List<T>, count: I64) -> Record{before: List<T>, others: List<T>}#: <T> fn(list: List<T>, count: I64) -> Record{before: List<T>, others: List<T>}List.split_at <- function(list, count)The first count elements as before and the rest as others.
List.split_at([1, 2, 3], 1)#> (before = [1i64], others = [2i64, 3i64])List.split_first
Section titled “List.split_first”List.split_first <- function<T>(list: List<T>, delimiter: T) -> Result<Record{before: List<T>, after: List<T>}, :NotFound>#: <T> fn(list: List<T>, delimiter: T) -> Result<Record{before: List<T>, after: List<T>}, :NotFound>List.split_first <- function(list, delimiter)Splits the list around the first element identical to delimiter.
List.split_first(["a", "-", "b", "-", "c"], "-")#> :Ok((after = ["b", "-", "c"], before = ["a"]))List.split_last
Section titled “List.split_last”List.split_last <- function<T>(list: List<T>, delimiter: T) -> Result<Record{before: List<T>, after: List<T>}, :NotFound>#: <T> fn(list: List<T>, delimiter: T) -> Result<Record{before: List<T>, after: List<T>}, :NotFound>List.split_last <- function(list, delimiter)Splits the list around the last element identical to delimiter.
List.split_last(["a", "-", "b", "-", "c"], "-")#> :Ok((after = ["c"], before = ["a", "-", "b"]))List.split_on
Section titled “List.split_on”List.split_on <- function<T>(list: List<T>, delimiter: T) -> List<List<T>>#: <T> fn(list: List<T>, delimiter: T) -> List<List<T>>List.split_on <- function(list, delimiter)The runs of elements between elements identical to delimiter.
List.split_on([1, 0, 2, 3, 0, 4], 0)#> [[1i64], [2i64, 3i64], [4i64]]List.split_on_list
Section titled “List.split_on_list”List.split_on_list <- function<T>(list: List<T>, delimiter: List<T>) -> List<List<T>>#: <T> fn(list: List<T>, delimiter: List<T>) -> List<List<T>>List.split_on_list <- function(list, delimiter)The runs of elements between occurrences of the sublist delimiter.
List.split_on_list([1, 0, 0, 2, 0, 3], [0, 0])#> [[1i64], [2i64, 0i64, 3i64]]List.chunks_of
Section titled “List.chunks_of”List.chunks_of <- function<T>(list: List<T>, size: I64) -> List<List<T>>#: <T> fn(list: List<T>, size: I64) -> List<List<T>>List.chunks_of <- function(list, size)Groups of size elements; the last group may be shorter.
List.chunks_of([1, 2, 3], 2)#> [[1i64, 2i64], [3i64]]List.intersperse
Section titled “List.intersperse”List.intersperse <- function<T>(list: List<T>, separator: T) -> List<T>#: <T> fn(list: List<T>, separator: T) -> List<T>List.intersperse <- function(list, separator)Puts separator between every two elements.
List.intersperse(["a", "b", "c"], "-")#> ["a", "-", "b", "-", "c"]List.sort_with
Section titled “List.sort_with”List.sort_with <- function<T>(list: List<T>, compare: fn(T, T) -> :LT | :EQ | :GT) -> List<T>#: <T> fn(list: List<T>, compare: fn(T, T) -> :LT | :EQ | :GT) -> List<T>List.sort_with <- function(list, compare)A stable sort by compare, which returns :LT, :EQ or :GT.
List.sort_with([3, 1, 2], Num.compare)#> [1i64, 2i64, 3i64]List.sort_asc
Section titled “List.sort_asc”List.sort_asc <- function<T: number>(list: List<T>) -> List<T>#: <T: number> fn(list: List<T>) -> List<T>List.sort_asc <- function(list)Sorts numbers from smallest to largest.
List.sort_asc([3, 1, 2])#> [1i64, 2i64, 3i64]List.sort_desc
Section titled “List.sort_desc”List.sort_desc <- function<T: number>(list: List<T>) -> List<T>#: <T: number> fn(list: List<T>) -> List<T>List.sort_desc <- function(list)Sorts numbers from largest to smallest.
List.sort_desc([1.5, 3, 2])#> [3, 2, 1.5]List.swap
Section titled “List.swap”List.swap <- function<T>(list: List<T>, first: I64, second: I64) -> List<T>#: <T> fn(list: List<T>, first: I64, second: I64) -> List<T>List.swap <- function(list, first, second)Exchanges the elements at two positions; an out-of-range position leaves the list unchanged.
List.swap(["a", "b", "c"], 1, 3)#> ["c", "b", "a"]List.replace
Section titled “List.replace”List.replace <- function<T>(list: List<T>, index: I64, value: T) -> Record{list: List<T>, value: T}#: <T> fn(list: List<T>, index: I64, value: T) -> Record{list: List<T>, value: T}List.replace <- function(list, index, value)Replaces the element at a position, returning the new list and the element it replaced.
List.replace(["a", "b"], 2, "z")#> (list = ["a", "z"], value = "b")List.append_if_ok
Section titled “List.append_if_ok”List.append_if_ok <- function<T, E>(list: List<T>, result: Result<T, E>) -> List<T>#: <T, E> fn(list: List<T>, result: Result<T, E>) -> List<T>List.append_if_ok <- function(list, result)Adds an :Ok value at the end; an :Err leaves the list unchanged.
List.append_if_ok([1], Str.to_i64("2"))#> [1i64, 2i64]List.prepend_if_ok
Section titled “List.prepend_if_ok”List.prepend_if_ok <- function<T, E>(list: List<T>, result: Result<T, E>) -> List<T>#: <T, E> fn(list: List<T>, result: Result<T, E>) -> List<T>List.prepend_if_ok <- function(list, result)Adds an :Ok value at the start; an :Err leaves the list unchanged.
List.prepend_if_ok([1], Str.to_i64("x"))#> [1i64]List.concat_utf8
Section titled “List.concat_utf8”List.concat_utf8 <- function(list: List<I64>, string: Str) -> List<I64>#: fn(list: List<I64>, string: Str) -> List<I64>List.concat_utf8 <- function(list, string)Appends the UTF-8 bytes of a string to a list of bytes.
List.concat_utf8([104], "i")#> [104i64, 105i64]List.with_capacity
Section titled “List.with_capacity”List.with_capacity <- function<T>(capacity: I64) -> List<T>#: <T> fn(capacity: I64) -> List<T>List.with_capacity <- function(capacity)An empty list with room for capacity elements, so appending that many never copies.
List.len(List.with_capacity(1000))#> 0i64List.reserve
Section titled “List.reserve”List.reserve <- function<T>(list: List<T>, spare: I64) -> List<T>#: <T> fn(list: List<T>, spare: I64) -> List<T>List.reserve <- function(list, spare)The list with room for spare more elements.
List.reserve([1, 2], 100)#> [1i64, 2i64]List.release_excess_capacity
Section titled “List.release_excess_capacity”List.release_excess_capacity <- function<T>(list: List<T>) -> List<T>#: <T> fn(list: List<T>) -> List<T>List.release_excess_capacity <- function(list)The list with no room beyond its elements, which gives back what List.reserve or
List.with_capacity set aside.
List.release_excess_capacity(List.reserve([1, 2], 100))#> [1i64, 2i64]List.sum
Section titled “List.sum”List.sum <- function<T: number>(list: List<T>) -> T#: <T: number> fn(list: List<T>) -> TList.sum <- function(list)The sum of the elements; 0L for an empty list. An integer sum that overflows is an error, so
sum doubles, or fold with Num.add_checked to get :Err(:Overflow) instead. A double sum
adds the elements from first to last in double precision, as a loop with + does. R’s sum()
accumulates with more precision, so the two can differ in the last digits.
List.sum([1, 2, 3])#> 6i64List.product
Section titled “List.product”List.product <- function<T: number>(list: List<T>) -> T#: <T: number> fn(list: List<T>) -> TList.product <- function(list)The product of the elements; 1L for an empty list. An integer product that overflows is an
error, as for List.sum.
List.product([2, 2.5])#> 5List.min
Section titled “List.min”List.min <- function<T: number>(list: List<T>) -> Result<T, :ListWasEmpty>#: <T: number> fn(list: List<T>) -> Result<T, :ListWasEmpty>List.min <- function(list)The smallest element, or :Err(:ListWasEmpty).
List.min([3, 1, 2])#> :Ok(1i64)List.max
Section titled “List.max”List.max <- function<T: number>(list: List<T>) -> Result<T, :ListWasEmpty>#: <T: number> fn(list: List<T>) -> Result<T, :ListWasEmpty>List.max <- function(list)The largest element, or :Err(:ListWasEmpty).
List.max([])#> :Err(:ListWasEmpty)List.starts_with
Section titled “List.starts_with”List.starts_with <- function<T>(list: List<T>, prefix: List<T>) -> Bool#: <T> fn(list: List<T>, prefix: List<T>) -> BoolList.starts_with <- function(list, prefix)Whether the list begins with the elements of prefix.
List.starts_with([1, 2, 3], [1, 2])#> TRUEList.ends_with
Section titled “List.ends_with”List.ends_with <- function<T>(list: List<T>, suffix: List<T>) -> Bool#: <T> fn(list: List<T>, suffix: List<T>) -> BoolList.ends_with <- function(list, suffix)Whether the list ends with the elements of suffix.
List.ends_with([1, 2, 3], [2])#> FALSE