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:52
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:63
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:77
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:88
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:102
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:117
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:137
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:148
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:159
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:170
reverse(values: List(a)) -> List(a)
INFERREDfn(List(a)) -> List(a)

Turn the list around in one pass through OTP, without an element callback.

Pooka
import pooka/list

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

map

src/pooka/list.pooka:182
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:195
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:214
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:233
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:247
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:264
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:279
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:290
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:301
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:326
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:343
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:356
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:393
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:412
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]
function

flatten

src/pooka/list.pooka:431
flatten(values: List(List(a))) -> List(a)
INFERREDfn(List(List(a))) -> List(a)

Let nested lists leave in a single queue, preserving both levels of order.

Pooka
import pooka/list
assert list.flatten([[1], [], [2, 3]]) == [1, 2, 3]
function

flat_map

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

Visit each item once, then join the returned lists in their original order.

Pooka
import pooka/list
assert list.flat_map([1, 2], fn(n) { [n, n] }) == [1, 1, 2, 2]
function

zip

src/pooka/list.pooka:459
zip(left: List(a), right: List(b)) -> List(#(a, b))
INFERREDfn(List(a), List(b)) -> List(#(a, b))

Pair the visitors while both queues have someone left. Stop at the shorter list.

Pooka
import pooka/list
assert list.zip([1, 2], ["owl"]) == [#(1, "owl")]
function

each

src/pooka/list.pooka:477
each(values: List(a), visit: fn(a) -> Nil) -> Nil
INFERREDfn(List(a), fn(a) -> Nil) -> Nil

Perform an effect for each item from left to right. The empty list returns Nil.

Pooka
import pooka/list
list.each([1, 2], fn(n) { assert n > 0 })
function

partition

src/pooka/list.pooka:490
partition(values: List(a), keep: fn(a) -> Bool) -> #(List(a), List(a))
INFERREDfn(List(a), fn(a) -> Bool) -> #(List(a), List(a))

Give matching and non-matching values separate seats, preserving both orders. The predicate runs once for every item.

Pooka
import pooka/list
assert list.partition([1, 2, 3], fn(n) { n > 1 }) == #([2, 3], [1])
function

split

src/pooka/list.pooka:508
split(values: List(a), count: Int) -> #(List(a), List(a))
INFERREDfn(List(a), Int) -> #(List(a), List(a))

Split before the zero-based position. Non-positive counts keep the whole list on the right; oversized counts leave an empty right side.

Pooka
import pooka/list
assert list.split([1, 2, 3], 2) == #([1, 2], [3])
function

split_while

src/pooka/list.pooka:530
split_while(values: List(a), keep: fn(a) -> Bool) -> #(List(a), List(a))
INFERREDfn(List(a), fn(a) -> Bool) -> #(List(a), List(a))

Keep the longest matching prefix. Stop asking the predicate at the first False; the remaining suffix is returned unchanged.

Pooka
import pooka/list
assert list.split_while([1, 2, 0, 3], fn(n) { n > 0 }) == #([1, 2], [0, 3])
function

find_map

src/pooka/list.pooka:550
find_map(values: List(a), find: fn(a) -> Option(b)) -> Option(b)
INFERREDfn(List(a), fn(a) -> Option(b)) -> Option(b)

Return the first Some produced by the callback. Later items are not visited.

Pooka
import pooka/list
assert list.find_map([1, 2], fn(n) { match n { 2 -> Some("owl") _ -> None } }) == Some("owl")
function

count

src/pooka/list.pooka:566
count(values: List(a), keep: fn(a) -> Bool) -> Int
INFERREDfn(List(a), fn(a) -> Bool) -> Int

Count the matching items, asking the predicate once for each one.

Pooka
import pooka/list
assert list.count([1, 2, 3], fn(n) { n > 1 }) == 2
function

repeat

src/pooka/list.pooka:581
repeat(value: a, count: Int) -> List(a)
INFERREDfn(a, Int) -> List(a)

Make a list containing count copies of value. Non-positive counts return [].

Pooka
import pooka/list
assert list.repeat("owl", 2) == ["owl", "owl"]
function

index_map

src/pooka/list.pooka:599
index_map(values: List(a), transform: fn(a, Int) -> b) -> List(b)
INFERREDfn(List(a), fn(a, Int) -> b) -> List(b)

Map from left to right with a zero-based index. The callback receives the value first and its index second.

Pooka
import pooka/list
assert list.index_map([10, 20], fn(n, index) { n + index }) == [10, 21]
function

index_fold

src/pooka/list.pooka:621
index_fold(values: List(a), initial: b, step: fn(b, a, Int) -> b) -> b
INFERREDfn(List(a), b, fn(b, a, Int) -> b) -> b

Fold with the accumulator, value and zero-based index, in that order.

Pooka
import pooka/list
assert list.index_fold([10, 20], 0, fn(acc, n, index) { acc + n + index }) == 31
function

key_find

src/pooka/list.pooka:638
key_find(pairs: List(#(k, v)), key: k) -> Option(v)
INFERREDfn(List(#(a, b)), a) -> Option(b)

Look up the first pair with this key. Missing keys return None.

Pooka
import pooka/list
assert list.key_find([#("owl", 2), #("owl", 3)], "owl") == Some(2)
function

try_map

src/pooka/list.pooka:658
try_map(values: List(a), transform: fn(a) -> Result(b, e)) -> Result(List(b), e)
INFERREDfn(List(a), fn(a) -> Result(b, c)) -> Result(List(b), c)

Map fallible work in order. The first Error returns immediately; remaining callbacks are not called. An empty list succeeds with Ok([]).

Pooka
import pooka/list
assert list.try_map([1, 2], fn(n) { Ok(n + 1) }) == Ok([2, 3])
function

try_fold

src/pooka/list.pooka:682
try_fold(values: List(a), initial: b, step: fn(b, a) -> Result(b, e)) -> Result(
  b,
  e
)
INFERREDfn(List(a), b, fn(b, a) -> Result(b, c)) -> Result(b, c)

Carry a fallible accumulator from left to right. The first Error stops the walk; an empty list returns Ok(initial).

Pooka
import pooka/list
assert list.try_fold([1, 2], 0, fn(acc, n) { Ok(acc + n) }) == Ok(3)
function

try_each

src/pooka/list.pooka:703
try_each(values: List(a), visit: fn(a) -> Result(Nil, e)) -> Result(Nil, e)
INFERREDfn(List(a), fn(a) -> Result(Nil, b)) -> Result(Nil, b)

Perform fallible effects in order. Stop at the first Error; the empty list succeeds with Ok(Nil).

Pooka
import pooka/list
assert list.try_each([1, 2], fn(n) { assert n > 0
  Ok(Nil) }) == Ok(Nil)