Module List

Finite list types and list combinators.

let xs = *(1, 2, 3)
xs->List.Map(type Int, box [n] n * 2)
type List.Builder<a> = iterative choice {
  .add => [a] self,
  .build => List<a>,
}

Incrementally constructs a list.

type List.Fallible<e, a> = recursive either {
  .end Result<e, !>,
  .item (a) self,
}

A list that ends with Result<e, !> instead of !. Use it for streams that may finish with an error.

type List<a> = recursive either {
  .end !,
  .item (a) self,
}

A finite, ordered sequence of values.

*(1, 2, 3)  // syntax sugar for .item(1) .item(2) .item(3) .end!
dec List.All : <a: box>[List<a>] [box [a] Bool] Bool

Returns .true! if the predicate holds for all elements. Short-circuits on the first .false!.

dec List.Any : <a: box>[List<a>] [box [a] Bool] Bool

Returns .true! if the predicate holds for at least one element. Short-circuits on the first .true!.

dec List.Concat : <a>[List<List<a>>] List<a>

Flattens a list of lists into a single list.

dec List.Copy : <a>[dual List<a>] [List<a>] dual List<a>

Yields all items from a source list onto a destination channel, consuming the source.

dec List.Drop : <a: box>[List<a>] [Nat] List<a>

Drops the first n elements of a list and returns the rest.

dec List.Filter : <a: box>[List<a>] [box [a] Bool] List<a>

Keeps the elements for which the predicate returns .true!.

dec List.FlatMap : <a>[List<a>] [type b, box [a] List<b>] List<b>

Applies f to each element and concatenates the resulting lists.

dec List.ForEach : <r>[r] <a>[List<a>] [box [r, a] r] r

Applies f repeatedly to the current result and each list element, returning the final result.

Expression syntax:

{0}->List.ForEach(*(1, 2, 3), box [sum, n] sum + n)
// = 6

Process syntax:

let console = Console.Open
console->List.ForEach(Nat.Range(1, 10), box [c, i]
  c.print(`#{i}`)
)
console.close
dec List.Length : <a: box>[List<a>] Nat

Returns the number of elements in a list.

dec List.Map : <a>[List<a>] [type b, box [a] b] List<b>

Applies f to each element of a list.

{*(1, 2, 3)}->List.Map(type Int, box [n] n * 2)
// = *(2, 4, 6)
dec List.Sort : <a: data>[List<a>] List<a>

Sorts a list in ascending data order. Equal keys keep their original order.

dec List.SortBy : <a: box>[List<a>] [type k: data, box [a] k] List<a>

Sorts a non-linear list by an extracted data key.

dec List.SortDesc : <a: data>[List<a>] List<a>

Sorts a list in descending data order. Equal keys keep their original order.

dec List.SortDescBy : <a: box>[List<a>] [type k: data, box [a] k] List<a>

Sorts a non-linear list by an extracted data key in descending order.

dec List.SortLinearBy : <a>[List<a>] [type k: data, box [a] (k) a] List<a>

Sorts a linear list by a function that returns both the key and the item.

dec List.SortLinearDescBy : <a>[List<a>] [type k: data, box [a] (k) a] List<a>

Sorts a linear list by a key in descending order.

dec List.Sum : <a: number>[List<a>] a

Calculates the sum of all elements in a list.

dec List.Take : <a: box>[List<a>] [Nat] List<a>

Returns the first n elements of a list.

dec List.Unzip : <a, b>[List<(a) b>] (List<a>) List<b>

Splits a list of pairs into a pair of lists.

dec List.Zip : <a: box>[List<a>] <b: box>[List<b>] List<(a) b>

Zips two lists into a list of pairs. Stops at the shorter list.