Skip to content
ry

List

Functions over R lists, typed list[T], and over Arrays. Each function also takes an Array<T> as its collection (the list, first or lists argument) and then returns an Array where it would return a list, so List.map([1, 2, 3], \(x) x * 2) is the Array<double> [2, 4, 6]. An item argument does not count, so List.append(list([1]), [2]) is the list list([1], [2]), and List.single and List.repeat always return lists. A function whose result items come from a callback, such as List.map, requires them to be Array items (the item constraint), so List.map([1, 2], \(x) list(x)) is a type error.

Function Summary
List.from_vector The elements of an atomic vector as a list.
List.to_vector The elements of a list as an atomic vector; every element must be a scalar of one type.
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_vector <- function<T>(vector: T[]) -> list[T]

The elements of an atomic vector as a list.

List.from_vector(c(3L, 1L, 2L))
#> list(3L, 1L, 2L)

Source

List.to_vector <- function<T>(list: list[T]) -> T[]

The elements of a list as an atomic vector; every element must be a scalar of one type.

List.to_vector(list(3L, 1L, 2L))
#> c(3L, 1L, 2L)

Source

List.len <- function<T>(list: Array<T>) -> integer
List.len <- function<T>(list: list[T]) -> integer

The number of elements.

List.len(list("a", "b"))
#> 2L

Source

List.is_empty <- function<T>(list: Array<T>) -> logical
List.is_empty <- function<T>(list: list[T]) -> logical

Whether the list has no elements.

List.is_empty(list())
#> TRUE

Source

List.get <- function<T>(list: Array<T>, index: integer) -> Result<T, :OutOfBounds>
List.get <- function<T>(list: list[T], index: integer) -> Result<T, :OutOfBounds>

The element at a 1-based position, or :Err(:OutOfBounds).

List.get(list("a", "b"), 3L)
#> :Err(:OutOfBounds)

Source

List.set <- function<T>(list: Array<T>, index: integer, value: T) -> Array<T>
List.set <- function<T>(list: list[T], index: integer, value: T) -> list[T]

Replaces the element at a position; a position out of range leaves the list unchanged.

List.set(list("a", "b"), 2L, "z")
#> list("a", "z")

Source

List.update <- function<T>(list: Array<T>, index: integer, f: fn(T) -> T) -> Array<T>
List.update <- function<T>(list: list[T], index: integer, f: fn(T) -> T) -> list[T]

Replaces the element at a position with f of it.

List.update(list(1L, 2L), 1L, function(x) x + 10L)
#> list(11L, 2L)

Source

List.append <- function<T>(list: Array<T>, value: T) -> Array<T>
List.append <- function<T>(list: list[T], value: T) -> list[T]

Adds an element at the end.

List.append(list(1L, 2L), 3L)
#> list(1L, 2L, 3L)

Source

List.prepend <- function<T>(list: Array<T>, value: T) -> Array<T>
List.prepend <- function<T>(list: list[T], value: T) -> list[T]

Adds an element at the start.

List.prepend(list(1L, 2L), 0L)
#> list(0L, 1L, 2L)

Source

List.concat <- function<T>(first: Array<T>, second: Array<T>) -> Array<T>
List.concat <- function<T>(first: list[T], second: list[T]) -> list[T]

The elements of first followed by those of second.

List.concat(list(1L), list(2L, 3L))
#> list(1L, 2L, 3L)

Source

List.single <- function<T>(value: T) -> list[T]

A list of one element.

List.single("only")
#> list("only")

Source

List.repeat <- function<T>(value: T, count: integer) -> list[T]

A list of count copies of value.

List.repeat("x", 3L)
#> list("x", "x", "x")

Source

List.range <- function(start: integer, end: integer) -> list[integer]

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(2L, 5L)
#> list(2L, 3L, 4L, 5L)
List.range(3L, 1L)
#> list()

Source

List.reverse <- function<T>(list: Array<T>) -> Array<T>
List.reverse <- function<T>(list: list[T]) -> list[T]

The elements in reverse order.

List.reverse(list(1L, 2L, 3L))
#> list(3L, 2L, 1L)

Source

List.join <- function<T>(lists: Array<Array<T>>) -> Array<T>
List.join <- function<T>(lists: list[list[T]]) -> list[T]

Concatenates a list of lists.

List.join(list(list(1L), list(), list(2L, 3L)))
#> list(1L, 2L, 3L)

Source

List.map <- function<A, B: item>(list: Array<A>, f: fn(A) -> B) -> Array<B>
List.map <- function<A, B>(list: list[A], f: fn(A) -> B) -> list[B]

Applies f to every element.

List.map(list(1L, 2L, 3L), function(x) x * 2L)
#> list(2L, 4L, 6L)

Source

List.map_with_index <- function<A, B: item>(list: Array<A>, f: fn(A, integer) -> B) -> Array<B>
List.map_with_index <- function<A, B>(list: list[A], f: fn(A, integer) -> B) -> list[B]

Applies f to every element and its 1-based position.

List.map_with_index(list("a", "b"), function(x, i) Str.concat(x, Num.to_str(i)))
#> list("a1", "b2")

Source

List.map2 <- function<A, B, C: item>(first: Array<A>, second: Array<B>, f: fn(A, B) -> C) -> Array<C>
List.map2 <- function<A, B, C>(first: list[A], second: list[B], f: fn(A, B) -> C) -> list[C]

Applies f to the elements of two lists pairwise, stopping at the shorter one.

List.map2(list(1L, 2L, 3L), list(10L, 20L), function(a, b) a + b)
#> list(11L, 22L)

Source

List.map3 <- function<A, B, C, D: item>(first: Array<A>, second: Array<B>, third: Array<C>, f: fn(A, B, C) -> D) -> Array<D>
List.map3 <- function<A, B, C, D>(first: list[A], second: list[B], third: list[C], f: fn(A, B, C) -> D) -> list[D]

Applies f to the elements at each position of three lists, up to the shortest one.

List.map3(list(1L, 2L), list(10L, 20L), list(100L, 200L), function(a, b, c) a + b + c)
#> list(111L, 222L)

Source

List.map4 <- function<A, B, C, D, E: item>(first: Array<A>, second: Array<B>, third: Array<C>, fourth: Array<D>, f: fn(A, B, C, D) -> E) -> Array<E>
List.map4 <- function<A, B, C, D, E>(first: list[A], second: list[B], third: list[C], fourth: list[D], f: fn(A, B, C, D) -> E) -> list[E]

Applies f to the elements at each position of four lists, up to the shortest one.

List.map4(list(1L), list(2L), list(3L), list(4L), function(a, b, c, d) a * b * c * d)
#> list(24L)

Source

List.join_map <- function<A, B>(list: Array<A>, f: fn(A) -> Array<B>) -> Array<B>
List.join_map <- function<A, B>(list: list[A], f: fn(A) -> list[B]) -> list[B]

Applies f, which returns a list, to every element and concatenates the results.

List.join_map(list(1L, 2L), function(x) list(x, x))
#> list(1L, 1L, 2L, 2L)

Source

List.keep_if <- function<T>(list: Array<T>, predicate: fn(T) -> logical) -> Array<T>
List.keep_if <- function<T>(list: list[T], predicate: fn(T) -> logical) -> list[T]

The elements for which predicate is TRUE.

List.keep_if(list(1L, 5L, 2L), function(x) x > 1L)
#> list(5L, 2L)

Source

List.keep_if_try <- function<T, E>(list: Array<T>, predicate: fn(T) -> Result<logical, E>) -> Result<Array<T>, E>
List.keep_if_try <- function<T, E>(list: list[T], predicate: fn(T) -> Result<logical, E>) -> Result<list[T], E>

The elements for which predicate returns :Ok(TRUE), or the first :Err it returns.

List.keep_if_try(list("1", "20", "3"), function(s) Result.map(Str.to_integer(s), function(n) n > 2L))
#> :Ok(list("20", "3"))
List.keep_if_try(list("1", "x"), function(s) Result.map(Str.to_integer(s), function(n) n > 2L))
#> :Err(:InvalidNumStr)

Source

List.drop_if <- function<T>(list: Array<T>, predicate: fn(T) -> logical) -> Array<T>
List.drop_if <- function<T>(list: list[T], predicate: fn(T) -> logical) -> list[T]

The elements for which predicate is FALSE.

List.drop_if(list(1L, 5L, 2L), function(x) x > 1L)
#> list(1L)

Source

List.keep_oks <- function<A, B: item, E>(list: Array<A>, f: fn(A) -> Result<B, E>) -> Array<B>
List.keep_oks <- function<A, B, E>(list: list[A], f: fn(A) -> Result<B, E>) -> list[B]

Applies f to every element and keeps the :Ok values.

List.keep_oks(list("1", "x", "3"), Str.to_integer)
#> list(1L, 3L)

Source

List.keep_errs <- function<A, B, E: item>(list: Array<A>, f: fn(A) -> Result<B, E>) -> Array<E>
List.keep_errs <- function<A, B, E>(list: list[A], f: fn(A) -> Result<B, E>) -> list[E]

Applies f to every element and keeps the :Err values.

List.keep_errs(list("1", "x"), Str.to_integer)
#> list(:InvalidNumStr)

Source

List.map_try <- function<A, B: item, E>(list: Array<A>, f: fn(A) -> Result<B, E>) -> Result<Array<B>, E>
List.map_try <- function<A, B, E>(list: list[A], f: fn(A) -> Result<B, E>) -> Result<list[B], E>

Applies f to every element and stops at the first :Err.

List.map_try(list("1", "2"), Str.to_integer)
#> :Ok(list(1L, 2L))

Source

List.walk <- function<T, S>(list: Array<T>, state: S, f: fn(S, T) -> S) -> S
List.walk <- function<T, S>(list: list[T], state: S, f: fn(S, T) -> S) -> S

Folds from the first element: f gets the state and an element and returns the next state.

List.walk(list(1L, 2L, 3L), 0L, function(total, x) total + x)
#> 6L

Source

List.walk_backwards <- function<T, S>(list: Array<T>, state: S, f: fn(S, T) -> S) -> S
List.walk_backwards <- function<T, S>(list: list[T], state: S, f: fn(S, T) -> S) -> S

Folds from the last element.

List.walk_backwards(list("a", "b", "c"), "", function(text, x) Str.concat(text, x))
#> "cba"

Source

List.walk_until <- function<T, S>(list: Array<T>, state: S, f: fn(S, T) -> :Continue(S) | :Break(S)) -> S
List.walk_until <- function<T, S>(list: list[T], state: S, f: fn(S, T) -> :Continue(S) | :Break(S)) -> S

Folds until f returns :Break(state); :Continue(state) goes on.

List.walk_until(list(1L, 2L, 3L, 4L), 0L, function(total, x) if (total >= 3L) :Break(total) else :Continue(total + x))
#> 3L

Source

List.walk_backwards_until <- function<T, S>(list: Array<T>, state: S, f: fn(S, T) -> :Continue(S) | :Break(S)) -> S
List.walk_backwards_until <- function<T, S>(list: list[T], state: S, f: fn(S, T) -> :Continue(S) | :Break(S)) -> S

Folds the list from the last element until f returns :Break(state).

List.walk_backwards_until(list(1L, 5L, 2L, 3L), 0L, function(total, x) if (x > 4L) :Break(total) else :Continue(total + x))
#> 5L

Source

List.walk_with_index <- function<T, S>(list: Array<T>, state: S, f: fn(S, T, integer) -> S) -> S
List.walk_with_index <- function<T, S>(list: list[T], state: S, f: fn(S, T, integer) -> S) -> S

Folds the list, passing f each element’s 1-based position too.

List.walk_with_index(list(10L, 20L, 30L), 0L, function(total, x, i) total + x * i)
#> 140L

Source

List.walk_with_index_until <- function<T, S>(list: Array<T>, state: S, f: fn(S, T, integer) -> :Continue(S) | :Break(S)) -> S
List.walk_with_index_until <- function<T, S>(list: list[T], state: S, f: fn(S, T, integer) -> :Continue(S) | :Break(S)) -> S

Folds the list with positions until f returns :Break(state).

List.walk_with_index_until(list("a", "b", "c"), "", function(seen, x, i) if (i > 2L) :Break(seen) else :Continue(Str.concat(seen, x)))
#> "ab"

Source

List.walk_from <- function<T, S>(list: Array<T>, index: integer, state: S, f: fn(S, T) -> S) -> S
List.walk_from <- function<T, S>(list: list[T], index: integer, state: S, f: fn(S, T) -> S) -> S

Folds the list from a 1-based position onwards.

List.walk_from(list(1L, 2L, 3L, 4L), 3L, 0L, function(total, x) total + x)
#> 7L

Source

List.walk_from_until <- function<T, S>(list: Array<T>, index: integer, state: S, f: fn(S, T) -> :Continue(S) | :Break(S)) -> S
List.walk_from_until <- function<T, S>(list: list[T], index: integer, state: S, f: fn(S, T) -> :Continue(S) | :Break(S)) -> S

Folds the list from a position until f returns :Break(state).

List.walk_from_until(list(1L, 2L, 3L, 4L), 2L, 0L, function(total, x) if (total >= 5L) :Break(total) else :Continue(total + x))
#> 5L

Source

List.walk_try <- function<T, S, E>(list: Array<T>, state: S, f: fn(S, T) -> Result<S, E>) -> Result<S, E>
List.walk_try <- function<T, S, E>(list: list[T], state: S, f: fn(S, T) -> Result<S, E>) -> Result<S, E>

Folds the list while f returns :Ok(state), stopping at the first :Err.

List.walk_try(list("1", "2", "x"), 0L, function(total, s) Result.map(Str.to_integer(s), function(n) total + n))
#> :Err(:InvalidNumStr)

Source

List.for_each <- function<T>(list: Array<T>, f: fn(T) -> Any) -> NULL
List.for_each <- function<T>(list: list[T], f: fn(T) -> Any) -> NULL

Calls f on each element for its effect, and returns NULL invisibly.

List.for_each(list("a", "b"), function(x) cat(x, "\n"))
#> a
#> b

Source

List.for_each_try <- function<T, E>(list: Array<T>, f: fn(T) -> Result<Any, E>) -> Result<NULL, E>
List.for_each_try <- function<T, E>(list: list[T], f: fn(T) -> Result<Any, E>) -> Result<NULL, E>

Calls f on each element, stopping at the first :Err.

List.for_each_try(list(1L, -2L, 3L), function(x) if (x < 0L) :Err(:Negative(x)) else :Ok(NULL))
#> :Err(:Negative(-2L))

Source

List.any <- function<T>(list: Array<T>, predicate: fn(T) -> logical) -> logical
List.any <- function<T>(list: list[T], predicate: fn(T) -> logical) -> logical

Whether predicate is TRUE for some element.

List.any(list(1L, 5L), function(x) x > 4L)
#> TRUE

Source

List.all <- function<T>(list: Array<T>, predicate: fn(T) -> logical) -> logical
List.all <- function<T>(list: list[T], predicate: fn(T) -> logical) -> logical

Whether predicate is TRUE for every element; TRUE for an empty list.

List.all(list(1L, 5L), function(x) x > 4L)
#> FALSE

Source

List.contains <- function<T>(list: Array<T>, value: T) -> logical
List.contains <- function<T>(list: list[T], value: T) -> logical

Whether some element is identical() to value.

List.contains(list("a", "b"), "b")
#> TRUE

Source

List.count_if <- function<T>(list: Array<T>, predicate: fn(T) -> logical) -> integer
List.count_if <- function<T>(list: list[T], predicate: fn(T) -> logical) -> integer

The number of elements for which predicate is TRUE.

List.count_if(list(1L, 5L, 7L), function(x) x > 4L)
#> 2L

Source

List.find_first <- function<T>(list: Array<T>, predicate: fn(T) -> logical) -> Result<T, :NotFound>
List.find_first <- function<T>(list: list[T], predicate: fn(T) -> logical) -> Result<T, :NotFound>

The first element for which predicate is TRUE, or :Err(:NotFound).

List.find_first(list(1L, 5L, 7L), function(x) x > 4L)
#> :Ok(5L)

Source

List.find_last <- function<T>(list: Array<T>, predicate: fn(T) -> logical) -> Result<T, :NotFound>
List.find_last <- function<T>(list: list[T], predicate: fn(T) -> logical) -> Result<T, :NotFound>

The last element for which predicate is TRUE, or :Err(:NotFound).

List.find_last(list(1L, 5L, 7L), function(x) x > 4L)
#> :Ok(7L)

Source

List.find_first_index <- function<T>(list: Array<T>, predicate: fn(T) -> logical) -> Result<integer, :NotFound>
List.find_first_index <- function<T>(list: list[T], predicate: fn(T) -> logical) -> Result<integer, :NotFound>

The position of the first match, or :Err(:NotFound).

List.find_first_index(list(1L, 5L, 7L), function(x) x > 4L)
#> :Ok(2L)

Source

List.find_last_index <- function<T>(list: Array<T>, predicate: fn(T) -> logical) -> Result<integer, :NotFound>
List.find_last_index <- function<T>(list: list[T], predicate: fn(T) -> logical) -> Result<integer, :NotFound>

The position of the last match, or :Err(:NotFound).

List.find_last_index(list(1L, 5L, 7L), function(x) x > 9L)
#> :Err(:NotFound)

Source

List.first <- function<T>(list: Array<T>) -> Result<T, :ListWasEmpty>
List.first <- function<T>(list: list[T]) -> Result<T, :ListWasEmpty>

The first element, or :Err(:ListWasEmpty).

List.first(list("a", "b"))
#> :Ok("a")

Source

List.last <- function<T>(list: Array<T>) -> Result<T, :ListWasEmpty>
List.last <- function<T>(list: list[T]) -> Result<T, :ListWasEmpty>

The last element, or :Err(:ListWasEmpty).

List.last(list())
#> :Err(:ListWasEmpty)

Source

List.take_first <- function<T>(list: Array<T>, count: integer) -> Array<T>
List.take_first <- function<T>(list: list[T], count: integer) -> list[T]

The first count elements, or all of them if there are fewer.

List.take_first(list(1L, 2L, 3L), 2L)
#> list(1L, 2L)

Source

List.take_last <- function<T>(list: Array<T>, count: integer) -> Array<T>
List.take_last <- function<T>(list: list[T], count: integer) -> list[T]

The last count elements.

List.take_last(list(1L, 2L, 3L), 2L)
#> list(2L, 3L)

Source

List.drop_first <- function<T>(list: Array<T>, count: integer) -> Array<T>
List.drop_first <- function<T>(list: list[T], count: integer) -> list[T]

Everything but the first count elements.

List.drop_first(list(1L, 2L, 3L), 2L)
#> list(3L)

Source

List.drop_last <- function<T>(list: Array<T>, count: integer) -> Array<T>
List.drop_last <- function<T>(list: list[T], count: integer) -> list[T]

Everything but the last count elements.

List.drop_last(list(1L, 2L, 3L), 2L)
#> list(1L)

Source

List.drop_at <- function<T>(list: Array<T>, index: integer) -> Array<T>
List.drop_at <- function<T>(list: list[T], index: integer) -> list[T]

Removes the element at a position.

List.drop_at(list("a", "b", "c"), 2L)
#> list("a", "c")

Source

List.sublist <- function<T>(list: Array<T>, start: integer, count: integer) -> Array<T>
List.sublist <- function<T>(list: list[T], start: integer, count: integer) -> list[T]

count elements starting at position start.

List.sublist(list(1L, 2L, 3L, 4L), 2L, 2L)
#> list(2L, 3L)

Source

List.split_at <- function<T>(list: Array<T>, count: integer) -> Record{before: Array<T>, others: Array<T>}
List.split_at <- function<T>(list: list[T], count: integer) -> Record{before: list[T], others: list[T]}

The first count elements as before and the rest as others.

List.split_at(list(1L, 2L, 3L), 1L)
#> (before = list(1L), others = list(2L, 3L))

Source

List.split_first <- function<T>(list: Array<T>, delimiter: T) -> Result<Record{before: Array<T>, after: Array<T>}, :NotFound>
List.split_first <- function<T>(list: list[T], delimiter: T) -> Result<Record{before: list[T], after: list[T]}, :NotFound>

Splits the list around the first element identical to delimiter.

List.split_first(list("a", "-", "b", "-", "c"), "-")
#> :Ok((after = list("b", "-", "c"), before = list("a")))

Source

List.split_last <- function<T>(list: Array<T>, delimiter: T) -> Result<Record{before: Array<T>, after: Array<T>}, :NotFound>
List.split_last <- function<T>(list: list[T], delimiter: T) -> Result<Record{before: list[T], after: list[T]}, :NotFound>

Splits the list around the last element identical to delimiter.

List.split_last(list("a", "-", "b", "-", "c"), "-")
#> :Ok((after = list("c"), before = list("a", "-", "b")))

Source

List.split_on <- function<T>(list: Array<T>, delimiter: T) -> Array<Array<T>>
List.split_on <- function<T>(list: list[T], delimiter: T) -> list[list[T]]

The runs of elements between elements identical to delimiter.

List.split_on(list(1L, 0L, 2L, 3L, 0L, 4L), 0L)
#> list(list(1L), list(2L, 3L), list(4L))

Source

List.split_on_list <- function<T>(list: Array<T>, delimiter: Array<T>) -> Array<Array<T>>
List.split_on_list <- function<T>(list: list[T], delimiter: list[T]) -> list[list[T]]

The runs of elements between occurrences of the sublist delimiter.

List.split_on_list(list(1L, 0L, 0L, 2L, 0L, 3L), list(0L, 0L))
#> list(list(1L), list(2L, 0L, 3L))

Source

List.chunks_of <- function<T>(list: Array<T>, size: integer) -> Array<Array<T>>
List.chunks_of <- function<T>(list: list[T], size: integer) -> list[list[T]]

Groups of size elements; the last group may be shorter.

List.chunks_of(list(1L, 2L, 3L), 2L)
#> list(list(1L, 2L), list(3L))

Source

List.intersperse <- function<T>(list: Array<T>, separator: T) -> Array<T>
List.intersperse <- function<T>(list: list[T], separator: T) -> list[T]

Puts separator between every two elements.

List.intersperse(list("a", "b", "c"), "-")
#> list("a", "-", "b", "-", "c")

Source

List.sort_with <- function<T>(list: Array<T>, compare: fn(T, T) -> :LT | :EQ | :GT) -> Array<T>
List.sort_with <- function<T>(list: list[T], compare: fn(T, T) -> :LT | :EQ | :GT) -> list[T]

A stable sort by compare, which returns :LT, :EQ or :GT.

List.sort_with(list(3L, 1L, 2L), Num.compare)
#> list(1L, 2L, 3L)

Source

List.sort_asc <- function<T: scalar numeric>(list: Array<T>) -> Array<T>
List.sort_asc <- function<T: scalar numeric>(list: list[T]) -> list[T]

Sorts numbers from smallest to largest.

List.sort_asc(list(3L, 1L, 2L))
#> list(1L, 2L, 3L)

Source

List.sort_desc <- function<T: scalar numeric>(list: Array<T>) -> Array<T>
List.sort_desc <- function<T: scalar numeric>(list: list[T]) -> list[T]

Sorts numbers from largest to smallest.

List.sort_desc(list(1.5, 3, 2))
#> list(3, 2, 1.5)

Source

List.swap <- function<T>(list: Array<T>, first: integer, second: integer) -> Array<T>
List.swap <- function<T>(list: list[T], first: integer, second: integer) -> list[T]

Exchanges the elements at two positions; an out-of-range position leaves the list unchanged.

List.swap(list("a", "b", "c"), 1L, 3L)
#> list("c", "b", "a")

Source

List.replace <- function<T>(list: Array<T>, index: integer, value: T) -> Record{list: Array<T>, value: T}
List.replace <- function<T>(list: list[T], index: integer, value: T) -> Record{list: list[T], value: T}

Replaces the element at a position, returning the new list and the element it replaced.

List.replace(list("a", "b"), 2L, "z")
#> (list = list("a", "z"), value = "b")

Source

List.append_if_ok <- function<T, E>(list: Array<T>, result: Result<T, E>) -> Array<T>
List.append_if_ok <- function<T, E>(list: list[T], result: Result<T, E>) -> list[T]

Adds an :Ok value at the end; an :Err leaves the list unchanged.

List.append_if_ok(list(1L), Str.to_integer("2"))
#> list(1L, 2L)

Source

List.prepend_if_ok <- function<T, E>(list: Array<T>, result: Result<T, E>) -> Array<T>
List.prepend_if_ok <- function<T, E>(list: list[T], result: Result<T, E>) -> list[T]

Adds an :Ok value at the start; an :Err leaves the list unchanged.

List.prepend_if_ok(list(1L), Str.to_integer("x"))
#> list(1L)

Source

List.concat_utf8 <- function(list: Array<integer>, string: character) -> Array<integer>
List.concat_utf8 <- function(list: list[integer], string: character) -> list[integer]

Appends the UTF-8 bytes of a string to a list of bytes.

List.concat_utf8(list(104L), "i")
#> list(104L, 105L)

Source

List.with_capacity <- function<T>(capacity: integer) -> list[T]

An empty list with room for capacity elements, so appending that many never copies.

List.len(List.with_capacity(1000L))
#> 0L

Source

List.reserve <- function<T>(list: Array<T>, spare: integer) -> Array<T>
List.reserve <- function<T>(list: list[T], spare: integer) -> list[T]

The list with room for spare more elements.

List.reserve(list(1L, 2L), 100L)
#> list(1L, 2L)

Source

List.release_excess_capacity <- function<T>(list: Array<T>) -> Array<T>
List.release_excess_capacity <- function<T>(list: list[T]) -> list[T]

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(list(1L, 2L), 100L))
#> list(1L, 2L)

Source

List.sum <- function<T: scalar numeric>(list: Array<T>) -> T
List.sum <- function<T: scalar numeric>(list: list[T]) -> T

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 is accumulated like R’s sum(), with more precision than a plain left-to-right fold, so List.sum(xs) and sum(xs) give the same number.

List.sum(list(1L, 2L, 3L))
#> 6L

Source

List.product <- function<T: scalar numeric>(list: Array<T>) -> T
List.product <- function<T: scalar numeric>(list: list[T]) -> T

The product of the elements; 1L for an empty list. An integer product that overflows is an error, as for List.sum.

List.product(list(2, 2.5))
#> 5

Source

List.min <- function<T: scalar numeric>(list: Array<T>) -> Result<T, :ListWasEmpty>
List.min <- function<T: scalar numeric>(list: list[T]) -> Result<T, :ListWasEmpty>

The smallest element, or :Err(:ListWasEmpty).

List.min(list(3L, 1L, 2L))
#> :Ok(1L)

Source

List.max <- function<T: scalar numeric>(list: Array<T>) -> Result<T, :ListWasEmpty>
List.max <- function<T: scalar numeric>(list: list[T]) -> Result<T, :ListWasEmpty>

The largest element, or :Err(:ListWasEmpty).

List.max(list())
#> :Err(:ListWasEmpty)

Source

List.starts_with <- function<T>(list: Array<T>, prefix: Array<T>) -> logical
List.starts_with <- function<T>(list: list[T], prefix: list[T]) -> logical

Whether the list begins with the elements of prefix.

List.starts_with(list(1L, 2L, 3L), list(1L, 2L))
#> TRUE

Source

List.ends_with <- function<T>(list: Array<T>, suffix: Array<T>) -> logical
List.ends_with <- function<T>(list: list[T], suffix: list[T]) -> logical

Whether the list ends with the elements of suffix.

List.ends_with(list(1L, 2L, 3L), list(2L))
#> FALSE

Source