Module

pooka/list

src/pooka/list.pooka

A list is a patient little queue; its old versions keep their places.

Build and transform immutable singly linked lists. Functions take the list first so a chain of transformations can travel through pipes. Indexes start at zero; ranges exclude their upper bound. Sorting is stable, and callbacks run in list order.

Pooka
import pooka/list

assert ([1, 2, 3] |> list.map(fn(x) { x * 2 })) == [2, 4, 6]

API reference

function

empty

src/pooka/list.pooka:35
empty() -> List(a)
INFERREDfn() -> List(a)

Return an empty list, ready to welcome elements of one inferred type.

Pooka
import pooka/list

assert list.empty() == []
function

is_empty

src/pooka/list.pooka:46
is_empty(values: List(a)) -> Bool
INFERREDfn(List(a)) -> Bool

Ask whether the list has no elements.

Pooka
import pooka/list

assert list.is_empty([])
function

length

src/pooka/list.pooka:60
length(values: List(a)) -> Int
INFERREDfn(List(a)) -> Int

Count elements from left to right; this walks the list.

Pooka
import pooka/list

assert list.length([1, 2, 3]) == 3
function

first

src/pooka/list.pooka:71
first(values: List(a)) -> Option(a)
INFERREDfn(List(a)) -> Option(a)

Open the first envelope, or return None for an empty list.

Pooka
import pooka/list

assert list.first([4, 5]) == Some(4)
function

last

src/pooka/list.pooka:85
last(values: List(a)) -> Option(a)
INFERREDfn(List(a)) -> Option(a)

Walk to the final element, or return None when there is none.

Pooka
import pooka/list

assert list.last([4, 5]) == Some(5)
function

at

src/pooka/list.pooka:100
at(values: List(a), index: Int) -> Option(a)
INFERREDfn(List(a), Int) -> Option(a)

Read a zero-based index. Negative and out-of-range indexes return None.

Pooka
import pooka/list

assert list.at([4, 5], 1) == Some(5)
function

prepend

src/pooka/list.pooka:120
prepend(values: List(a), value: a) -> List(a)
INFERREDfn(List(a), a) -> List(a)

Put a value at the front without walking the existing list.

Pooka
import pooka/list

assert list.prepend([2, 3], 1) == [1, 2, 3]
function

append

src/pooka/list.pooka:131
append(values: List(a), value: a) -> List(a)
INFERREDfn(List(a), a) -> List(a)

Put a value at the end, rebuilding the path through the original list.

Pooka
import pooka/list

assert list.append([1, 2], 3) == [1, 2, 3]
function

concat

src/pooka/list.pooka:142
concat(left: List(a), right: List(a)) -> List(a)
INFERREDfn(List(a), List(a)) -> List(a)

Join two lists while retaining their element order.

Pooka
import pooka/list

assert list.concat([1, 2], [3]) == [1, 2, 3]
function

reverse

src/pooka/list.pooka:155
reverse(values: List(a)) -> List(a)
INFERREDfn(List(a)) -> List(a)

Turn the list around using a tail-recursive fold.

Pooka
import pooka/list

assert list.reverse([1, 2, 3]) == [3, 2, 1]
function

map

src/pooka/list.pooka:167
map(values: List(a), transform: fn(a) -> b) -> List(b)
INFERREDfn(List(a), fn(a) -> b) -> List(b)

Visit each element once and keep the callback results in their original order.

Pooka
import pooka/list

assert list.map([1, 2], fn(x) { x * 2 }) == [2, 4]
function

filter

src/pooka/list.pooka:180
filter(values: List(a), keep: fn(a) -> Bool) -> List(a)
INFERREDfn(List(a), fn(a) -> Bool) -> List(a)

Keep the elements whose predicate returns True, preserving their order.

Pooka
import pooka/list

assert list.filter([1, 2, 3], fn(x) { x > 1 }) == [2, 3]
function

filter_map

src/pooka/list.pooka:199
filter_map(values: List(a), transform: fn(a) -> Option(b)) -> List(b)
INFERREDfn(List(a), fn(a) -> Option(b)) -> List(b)

Let each callback either send a transformed value onward or quietly return None.

Pooka
import pooka/list

assert list.filter_map([1, 2], fn(x) { Some(x + 1) }) == [2, 3]
function

fold

src/pooka/list.pooka:218
fold(values: List(a), initial: b, step: fn(b, a) -> b) -> b
INFERREDfn(List(a), b, fn(b, a) -> b) -> b

Carry an accumulator through the list. The callback receives accumulator first, element second.

Pooka
import pooka/list

assert list.fold([1, 2, 3], 0, fn(acc, x) { acc + x }) == 6
function

find

src/pooka/list.pooka:232
find(values: List(a), predicate: fn(a) -> Bool) -> Option(a)
INFERREDfn(List(a), fn(a) -> Bool) -> Option(a)

Stop at the first element accepted by the predicate.

Pooka
import pooka/list

assert list.find([1, 2, 3], fn(x) { x > 1 }) == Some(2)
function

any

src/pooka/list.pooka:249
any(values: List(a), predicate: fn(a) -> Bool) -> Bool
INFERREDfn(List(a), fn(a) -> Bool) -> Bool

Return True as soon as one element satisfies the predicate.

Pooka
import pooka/list

assert list.any([1, 2], fn(x) { x == 2 })
function

all

src/pooka/list.pooka:264
all(values: List(a), predicate: fn(a) -> Bool) -> Bool
INFERREDfn(List(a), fn(a) -> Bool) -> Bool

Require every element to satisfy the predicate. The empty list has no counterexample.

Pooka
import pooka/list

assert list.all([], fn(x: Int) { x > 0 })
function

contains

src/pooka/list.pooka:275
contains(values: List(a), value: a) -> Bool
INFERREDfn(List(a), a) -> Bool

Test membership with the language's equality operation.

Pooka
import pooka/list

assert list.contains([1, 2], 2)
function

take

src/pooka/list.pooka:286
take(values: List(a), count: Int) -> List(a)
INFERREDfn(List(a), Int) -> List(a)

Keep at most count elements. A nonpositive count produces an empty list.

Pooka
import pooka/list

assert list.take([1, 2, 3], 2) == [1, 2]
function

drop

src/pooka/list.pooka:311
drop(values: List(a), count: Int) -> List(a)
INFERREDfn(List(a), Int) -> List(a)

Discard at most count elements. A nonpositive count leaves the list alone.

Pooka
import pooka/list

assert list.drop([1, 2, 3], 2) == [3]
function

sort

src/pooka/list.pooka:328
sort(values: List(a)) -> List(a)
INFERREDfn(List(a)) -> List(a)

Stable-sort using the Erlang term order exposed by the comparison primitive.

Pooka
import pooka/list

assert list.sort([3, 1, 2]) == [1, 2, 3]
function

sort_by

src/pooka/list.pooka:341
sort_by(values: List(a), compare: fn(a, a) -> Order) -> List(a)
INFERREDfn(List(a), fn(a, a) -> pooka/order.Order) -> List(a)

Stable-sort using a callback returning Order; equal elements retain their relative order.

Pooka
import pooka/list
import pooka/order.{Order}

assert list.sort_by([3, 1, 2], fn(a, b) { order.from_int(a - b) }) == [1, 2, 3]
function

unique

src/pooka/list.pooka:378
unique(values: List(a)) -> List(a)
INFERREDfn(List(a)) -> List(a)

Keep the first occurrence of each value. Equality is checked against values already kept.

Pooka
import pooka/list

assert list.unique([2, 1, 2, 1]) == [2, 1]
function

range

src/pooka/list.pooka:397
range(start: Int, end: Int) -> List(Int)
INFERREDfn(Int, Int) -> List(Int)

Count upward from start, stopping before end. Reversed or empty bounds produce an empty list.

Pooka
import pooka/list

assert list.range(2, 5) == [2, 3, 4]