Skip to content
Menu
Contents

Reading the tags

pure

Same arguments, same result; touches nothing outside.

pure if arg2 is

Pure when that argument is a pure function; otherwise as impure as it is.

impure

Reads or changes something outside the program.

List

1 type17 functions

Functions on List(a), the singly linked list. A list literal [1, 2, 3] is built from empty and cons, and a match takes one apart with list patterns such as [x, ..rest]. Functions that could fail on an empty list or a missing element answer a Result instead of trapping.

[1, 2, 3, 4]
  |> List.filter(\(n) -> n % 2 == 0)
  |> List.foldl(0, Int.add)
Returns
6

At a glance

  • Result

    Shorthand for [Ok(a) | Err(e)], the variant this module's fallible functions return.

  • empty

    The empty list.

  • cons

    The list with head in front of tail.

  • head

    Total: an empty list is an Err rather than a trap.

  • tail

    The tail of the empty list is the empty list.

  • length

    The number of elements.

  • map

    Each element passed through f, in order.

  • filter

    The elements the predicate holds for, in their order.

  • foldl

    foldl([x0, x1], init, f) is f(f(init, x0), x1).

  • forEach

    map for a callback run only for what it does, building no list.

  • nth

    Total: an index outside the list is an Err rather than a trap.

  • reverse

    The elements in the opposite order.

  • concat

    The elements of the first list, then those of the second.

  • flatMap

    Each element's list from f, joined in order.

  • find

    The first element the predicate holds for.

  • any

    any([], p) is false.

  • all

    all([], p) is true.

  • sort

    A stable sort: elements the comparator calls equal keep their order.

Types

Result

Result(a, e) = [Ok(a) | Err(e)]

Shorthand for [Ok(a) | Err(e)], the variant this module's fallible functions return.

Functions

empty

() -> List(a)pure

The empty list. A list literal is this and cons: [1, 2] is cons(1, cons(2, empty())).

cons

(a, List(a)) -> List(a)pure

The list with head in front of tail. A list literal is this and empty: [1, 2] is cons(1, cons(2, empty())).

(List(a)) -> Result(a, [EmptyList | e])pure

Total: an empty list is an Err rather than a trap.

List.head([4, 5])
Returns
Ok(4)
List.head([])
Returns
Err(EmptyList)

tail

(List(a)) -> List(a)pure

The tail of the empty list is the empty list.

length

(List(a)) -> Intpure

The number of elements.

map

(List(a), (a) -> b) -> List(b)pure if arg2 is

Each element passed through f, in order.

List.map([1, 2, 3], \(n) -> n * 2)
Returns
[2, 4, 6]

filter

(List(a), (a) -> Bool) -> List(a)pure if arg2 is

The elements the predicate holds for, in their order.

List.filter([1, 2, 3, 4], \(n) -> n % 2 == 0)
Returns
[2, 4]

foldl

(List(a), b, (b, a) -> b) -> bpure if arg3 is

foldl([x0, x1], init, f) is f(f(init, x0), x1).

List.foldl([1, 2, 3], 0, \(total, n) -> total + n)
Returns
6

forEach

(List(a), (a) -> Unit) -> Unitpure if arg2 is

map for a callback run only for what it does, building no list.

nth

(List(a), Int) -> Result(a, [OutOfBounds | e])pure

Total: an index outside the list is an Err rather than a trap.

reverse

(List(a)) -> List(a)pure

The elements in the opposite order.

concat

(List(a), List(a)) -> List(a)pure

The elements of the first list, then those of the second.

flatMap

(List(a), (a) -> List(b)) -> List(b)pure if arg2 is

Each element's list from f, joined in order.

find

(List(a), (a) -> Bool) -> Result(a, [NotFound | e])pure if arg2 is

The first element the predicate holds for.

any

(List(a), (a) -> Bool) -> Boolpure if arg2 is

any([], p) is false.

all

(List(a), (a) -> Bool) -> Boolpure if arg2 is

all([], p) is true.

sort

(List(a), (a, a) -> Int) -> List(a)pure if arg2 is

A stable sort: elements the comparator calls equal keep their order. The comparator answers a sign, negative when its first argument goes first.

Reading the tags

pure

Same arguments, same result; touches nothing outside.

pure if arg2 is

Pure when that argument is a pure function; otherwise as impure as it is.

impure

Reads or changes something outside the program.