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:54
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:65
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:79
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:90
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:104
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:119
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:139
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:150
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:161
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:172
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:184
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:207
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:226
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:237
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:254
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:269
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:280
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:291
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:316
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:333
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:360
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]

A string field can lead the procession too. Equal names keep their order.

Pooka
import pooka/list
import pooka/string

class Owl(name: String)

let owls = [Owl("Fig"), Owl("Ada")]
assert list.sort_by(owls, fn(a: Owl, b: Owl) {
  string.compare(a.name, b.name)
}) == [Owl("Ada"), Owl("Fig")]
function

unique

src/pooka/list.pooka:397
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:416
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:435
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:449
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:463
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:481
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:492
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:510
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:532
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:552
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:568
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:583
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:601
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:623
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:640
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:660
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:684
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:705
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)
function

take_while

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

Keep the longest matching prefix. The first False ends the visit.

Pooka
assert list.take_while([1, 2, 0, 3], fn(n) { n > 0 }) == [1, 2]
function

drop_while

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

Return the suffix after the longest matching prefix.

Pooka
assert list.drop_while([1, 2, 0, 3], fn(n) { n > 0 }) == [0, 3]
function

chunk

src/pooka/list.pooka:745
chunk(values: List(a), size: Int) -> List(List(a))
INFERREDfn(List(a), Int) -> List(List(a))

Divide into nonempty chunks. Non-positive sizes produce no chunks.

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

group

src/pooka/list.pooka:767
group(values: List(a), key: fn(a) -> k) -> dict.Dict(k, List(a))
INFERREDfn(List(a), fn(a) -> b) -> pooka/dict.Dict(b, List(a))

Group equal keys in a dictionary; each group keeps input order.

Pooka
assert list.group([1, 2, 3], fn(n) { n % 2 }).get(1) == Some([1, 3])
class

FoldStep

src/pooka/list.pooka:780
class FoldStep(value: a)

A fold may continue with an accumulator or stop with its final value. The base FoldStep constructor continues, just like Continue.

field

value

src/pooka/list.pooka:780
value: a
INFERREDa
constructor

new

src/pooka/list.pooka:780
INFERREDfn(a) -> pooka/list.FoldStep(a)
class

Continue

src/pooka/list.pooka:783
class Continue(a) is FoldStep(a)

Continue visiting values with this accumulator.

constructor

new

src/pooka/list.pooka:784
new(value: a)
INFERREDfn(a) -> pooka/list.Continue(a)
class

Stop

src/pooka/list.pooka:790
class Stop(a) is FoldStep(a)

Stop visiting values and return this accumulator.

constructor

new

src/pooka/list.pooka:791
new(value: a)
INFERREDfn(a) -> pooka/list.Stop(a)
function

fold_until

src/pooka/list.pooka:801
fold_until(values: List(a), initial: b, step: fn(b, a) -> FoldStep(b)) -> b
INFERREDfn(List(a), b, fn(b, a) -> pooka/list.FoldStep(b)) -> b

Fold until Stop, without visiting the remaining values.

Pooka
assert list.fold_until([1, 2], 0, fn(acc, n) { list.Stop(acc + n) }) == 1
function

reduce

src/pooka/list.pooka:817
reduce(values: List(a), step: fn(a, a) -> a) -> Option(a)
INFERREDfn(List(a), fn(a, a) -> a) -> Option(a)

Fold a nonempty list using its first element as the initial accumulator.

Pooka
assert list.reduce([1, 2, 3], fn(a, b) { a + b }) == Some(6)
function

chunk_by

src/pooka/list.pooka:829
chunk_by(values: List(a), key: fn(a) -> k) -> List(List(a))
INFERREDfn(List(a), fn(a) -> b) -> List(List(a))

Group consecutive elements with equal keys, keeping separate later runs.

Pooka
assert list.chunk_by([1, 3, 2, 4, 5], fn(n) { n % 2 }) == [[1, 3], [2, 4], [5]]