Std.List (Cure Standard Library v0.37.2)

View Source

Eager, persistent, singly-linked lists.

All operations are recursive over cons cells; mutation is not possible. Helpers come in five flavours: queries, access, construction, transformations, and search. Everything is polymorphic in the element type T.

Examples

use Std.List

let xs = [1, 2, 3, 4, 5]
length(xs)                                # => 5
map(xs, fn(x) -> x * x)                   # => [1, 4, 9, 16, 25]
filter(xs, fn(x) -> x % 2 == 0)           # => [2, 4]
use Std.List

# Sum of squares of positive integers up to 10
let r = (1..11)                           # [1..10]
          |> filter(fn(x) -> x > 0)
          |> map(fn(x) -> x * x)
          |> sum
# r => 385
use Std.List

match uncons([1, 2, 3])
  Some(p) -> Std.Io.print_int(p.1)         # prints 1
  None()  -> Std.Io.println("empty")

Summary

Types

Group tag consumed by Cure.Stdlib.Preload. The canonical cons-list family. Seeded programmatically into every module (so []/[h | t] literal sugar resolves without use), and declared here with the builtin decorator; builtin_list_drift_test.exs pins the two equal.

Functions

true when every element satisfies pred (vacuously true for []).

true when at least one element satisfies pred.

Concatenate two lists. Linear in length(a).

0-based lookup as an Option: Some(element) when 0 <= idx < length, None() otherwise (past either end, on [], or a negative index). The total, honest counterpart to nth/3 — no default to invent. Structural recursion; a negative index falls off the end and yields None().

Concatenate a list of lists into a single flat list.

Prepend elem onto list (the Cure-level spelling of [elem | list]).

true when elem appears in list (structural equality). The requires Equatable(t) constraint threads the dictionary that dispatches the h == elem comparison at the element type.

Count of elements satisfying pred.

Drop the first n elements; returns list unchanged when n <= 0.

Keep elements of list for which pred(elem) returns true.

First element satisfying pred; returns default when nothing matches.

Map f over list and flatten one level of nesting.

Left fold. f is curried: f(elem)(acc) -> acc. Reduces list starting from the head.

Right fold. f is curried: f(elem)(acc) -> acc. Builds up the result from the tail toward the head; not tail-recursive, so expect O(length) stack on very long lists.

First element of list; returns default when empty.

true when list has no elements.

Last element of list; returns default when empty.

Length of list as an Int. Delegates to :erlang.length/1.

Map f over every element of list.

0-based random access. Returns default when idx is out of range.

Product of an Int list, left-folded.

Reverse list; tail-recursive via a private accumulator.

Replace the element at idx with value when 0 <= idx < length; return list unchanged otherwise (past either end, on [], or a negative index). Total; structural recursion. This is the putter half of the ix affine.

Like uncons/1, but substitutes default for the head when empty, so the result is always a %[head, tail] pair.

Sum of an Int list, left-folded.

All-but-the-first elements of list; empty list yields [].

First n elements; [] when n <= 0.

Split list into Some(%[head, tail]), or None() when empty.

Combine two lists element-wise using f. Stops at the shorter list. f is curried: f(a)(b) -> c.

Types

type List(l, a) indices ()

 type List(l, a) indices ()
   Nil : List(l, a)
   Cons : a -> List(l, a) -> List(l, a)

Group tag consumed by Cure.Stdlib.Preload. The canonical cons-list family. Seeded programmatically into every module (so []/[h | t] literal sugar resolves without use), and declared here with the builtin decorator; builtin_list_drift_test.exs pins the two equal.

Functions

all(list: List(t), pred: Function(t, Bool)) -> Bool

 fn all(list: List(t), pred: Function(t, Bool)) -> Bool

true when every element satisfies pred (vacuously true for []).

any(list: List(t), pred: Function(t, Bool)) -> Bool

 fn any(list: List(t), pred: Function(t, Bool)) -> Bool

true when at least one element satisfies pred.

append(a: List(t), b: List(t)) -> List(t)

 fn append(a: List(t), b: List(t)) -> List(t)

Concatenate two lists. Linear in length(a).

at(list: List(t), idx: Int) -> Option(t)

 fn at(list: List(t), idx: Int) -> Option(t)

0-based lookup as an Option: Some(element) when 0 <= idx < length, None() otherwise (past either end, on [], or a negative index). The total, honest counterpart to nth/3 — no default to invent. Structural recursion; a negative index falls off the end and yields None().

concat(lists: List(List(t))) -> List(t)

 fn concat(lists: List(List(t))) -> List(t)

Concatenate a list of lists into a single flat list.

cons(elem: t, list: List(t)) -> List(t)

 fn cons(elem: t, list: List(t)) -> List(t)

Prepend elem onto list (the Cure-level spelling of [elem | list]).

contains(list: List(t), elem: t) -> Bool

 fn contains(list: List(t), elem: t) -> Bool

true when elem appears in list (structural equality). The requires Equatable(t) constraint threads the dictionary that dispatches the h == elem comparison at the element type.

count(list: List(t), pred: Function(t, Bool)) -> Int

 fn count(list: List(t), pred: Function(t, Bool)) -> Int

Count of elements satisfying pred.

drop(list: List(t), n: Int) -> List(t)

 fn drop(list: List(t), n: Int) -> List(t)

Drop the first n elements; returns list unchanged when n <= 0.

drop_rest(list: List(t), n: Int) -> List(t)

 fn drop_rest(list: List(t), n: Int) -> List(t)

Private recursive step for drop/2.

filter(list: List(t), pred: Function(t, Bool)) -> List(t)

 fn filter(list: List(t), pred: Function(t, Bool)) -> List(t)

Keep elements of list for which pred(elem) returns true.

find(list: List(t), pred: Function(t, Bool), default: t) -> t

 fn find(list: List(t), pred: Function(t, Bool), default: t) -> t

First element satisfying pred; returns default when nothing matches.

flat_map(list: List(t), f: Function(t, List(u))) -> List(u)

 fn flat_map(list: List(t), f: Function(t, List(u))) -> List(u)

Map f over list and flatten one level of nesting.

foldl(list: List(t), acc: u, f: Function(t, Function(u, u))) -> u

 fn foldl(list: List(t), acc: u, f: Function(t, Function(u, u))) -> u

Left fold. f is curried: f(elem)(acc) -> acc. Reduces list starting from the head.

foldr(list: List(t), acc: u, f: Function(t, Function(u, u))) -> u

 fn foldr(list: List(t), acc: u, f: Function(t, Function(u, u))) -> u

Right fold. f is curried: f(elem)(acc) -> acc. Builds up the result from the tail toward the head; not tail-recursive, so expect O(length) stack on very long lists.

head(list: List(t), default: t) -> t

 fn head(list: List(t), default: t) -> t

First element of list; returns default when empty.

is_empty(list: List(t)) -> Bool

 fn is_empty(list: List(t)) -> Bool

true when list has no elements.

last(list: List(t), default: t) -> t

 fn last(list: List(t), default: t) -> t

Last element of list; returns default when empty.

length(list: List(t)) -> Int

 fn length(list: List(t)) -> Int

Length of list as an Int. Delegates to :erlang.length/1.

map(list: List(t), f: Function(t, u)) -> List(u)

 fn map(list: List(t), f: Function(t, u)) -> List(u)

Map f over every element of list.

nth(list: List(t), idx: Int, default: t) -> t

 fn nth(list: List(t), idx: Int, default: t) -> t

0-based random access. Returns default when idx is out of range.

product(list: List(Int)) -> Int

 fn product(list: List(Int)) -> Int

Product of an Int list, left-folded.

reverse(list: List(t)) -> List(t)

 fn reverse(list: List(t)) -> List(t)

Reverse list; tail-recursive via a private accumulator.

reverse_acc(list: List(t), acc: List(t)) -> List(t)

 fn reverse_acc(list: List(t), acc: List(t)) -> List(t)

Private accumulator helper for reverse/1.

set_at(list: List(t), idx: Int, value: t) -> List(t)

 fn set_at(list: List(t), idx: Int, value: t) -> List(t)

Replace the element at idx with value when 0 <= idx < length; return list unchanged otherwise (past either end, on [], or a negative index). Total; structural recursion. This is the putter half of the ix affine.

split_first(list: List(t), default: t) -> Tuple(t, List(t))

 fn split_first(list: List(t), default: t) -> Tuple(t, List(t))

Like uncons/1, but substitutes default for the head when empty, so the result is always a %[head, tail] pair.

sum(list: List(Int)) -> Int

 fn sum(list: List(Int)) -> Int

Sum of an Int list, left-folded.

tail(list: List(t)) -> List(t)

 fn tail(list: List(t)) -> List(t)

All-but-the-first elements of list; empty list yields [].

take(list: List(t), n: Int) -> List(t)

 fn take(list: List(t), n: Int) -> List(t)

First n elements; [] when n <= 0.

take_rest(list: List(t), n: Int) -> List(t)

 fn take_rest(list: List(t), n: Int) -> List(t)

Private recursive step for take/2.

uncons(list: List(t)) -> Option(Tuple(t, List(t)))

 fn uncons(list: List(t)) -> Option(Tuple(t, List(t)))

Split list into Some(%[head, tail]), or None() when empty.

zip_with(a: List(t), b: List(u), f: Function(t, Function(u, v))) -> List(v)

 fn zip_with(a: List(t), b: List(u), f: Function(t, Function(u, v))) -> List(v)

Combine two lists element-wise using f. Stops at the shorter list. f is curried: f(a)(b) -> c.