.nib std
nib 0.3.0

module always in scope

Vec

An ordered sequence of values of one type, written #[1, 2, 3]. Every operation answers a new Vec; none changes the one it was given.

Vec<'t> is nib's workhorse collection: indexed from 0, every element the same type, and immutable — Vec.push(v, x) answers a longer Vec and leaves v as it was. That is what lets a module hold one without being able to change anything.

The operations are functions in the Vec namespace, always in scope, and they take the Vec first. A function argument comes last and is an ordinary closure, { x -> x * 2 }. Callbacks are pure: the engine is free to run a map or a fold over a flat Vec in parallel, and nothing in the program can tell.

let scores = #[72, 95, 88, 61];
let passed = Vec.filter(scores, { s -> s >= 70 });
let total = Vec.fold(passed, 0, { acc, s -> acc + s });
(Vec.size(passed), total)
(3, 255)

Reads that can miss

Vec.get, Vec.first, Vec.last, Vec.set and Vec.slice raise Err.IndexOutOfRange for an index outside the Vec, and the checker makes the caller say so: either declare it in a raises row or catch it with try … rescue. When "not there" is an ordinary answer rather than a bug, Vec.Opt asks the same questions and answers an Option instead.

try Vec.get(#["a", "b"], 5)
rescue Err.IndexOutOfRange(i, msg) => "nothing at {i}: {msg}" end
nothing at 5: index 5 is outside a Vec of 2

Functions

funcsize
func size :: Vec<'a> -> Int

The number of elements.

Vec.size(#["a", "b", "c"])
3
funcget
func get :: Vec<'a>, Int -> 'a raises Err.IndexOutOfRange

The element at index i, counting from 0. Raises Err.IndexOutOfRange when i is negative or not less than Vec.size; Vec.Opt.get answers None instead.

Vec.get(#[10, 20, 30], 1)
20
funcpush
func push :: Vec<'a>, 'a -> Vec<'a>

A new Vec with x added at the end.

let v = #[1, 2];
let w = Vec.push(v, 3);
(v, w)
(#[1, 2], #[1, 2, 3])
funcset
func set :: Vec<'a>, Int, 'a -> Vec<'a> raises Err.IndexOutOfRange

A new Vec with the element at i replaced by x. Raises Err.IndexOutOfRange when i is outside the Vec — set replaces, it never grows.

Vec.set(#["a", "b", "c"], 1, "B")
#["a", "B", "c"]
funcslice
func slice :: Vec<'a>, Int, Int -> Vec<'a> raises Err.IndexOutOfRange

The elements from index lo up to, but not including, hi. Raises Err.IndexOutOfRange when lo is greater than hi or the range runs past either end; an empty range such as slice(v, 3, 3) is fine. Vec.take and Vec.drop clamp instead of raising.

Vec.slice(#[0, 1, 2, 3, 4], 1, 4)
#[1, 2, 3]
funcmap
func map :: Vec<'a>, ('a -> 'b) -> Vec<'b>

Applies f to every element and answers the results, in order.

Vec.map(#[1, 2, 3], { x -> x * 10 })
#[10, 20, 30]
funcfilter
func filter :: Vec<'a>, ('a -> Bool) -> Vec<'a>

The elements for which p answers true, in their original order.

Vec.filter(#[1, 2, 3, 4, 5, 6], { x -> x % 2 == 0 })
#[2, 4, 6]
funcfold
func fold :: Vec<'a>, 'b, (('b, 'a) -> 'b) -> 'b

Combines the elements from the first to the last: f(acc, x) is called once per element, starting from init, and its answer is the next acc. The final acc is the result.

fold is the loop the machine itself runs — a raise from inside f can be caught by a try around the fold with the walk's state intact.

Vec.fold(#["n", "i", "b"], "", { acc, s -> acc ++ s })
nib
func fold_right :: Vec<'a>, 'b, (('a, 'b) -> 'b) -> 'b

Like Vec.fold, but from the last element to the first, and f takes the element first: f(x, acc).

Vec.fold_right(#["n", "i", "b"], "", { s, acc -> acc ++ s })
bin
funcfirst
func first :: Vec<'a> -> 'a raises Err.IndexOutOfRange

The first element. Raises Err.IndexOutOfRange on an empty Vec; Vec.Opt.first answers None instead.

Vec.first(#["head", "tail"])
head
funclast
func last :: Vec<'a> -> 'a raises Err.IndexOutOfRange

The last element. Raises Err.IndexOutOfRange on an empty Vec; Vec.Opt.last answers None instead.

Vec.last(#["head", "tail"])
tail
functake
func take :: Vec<'a>, Int -> Vec<'a>

The first n elements — or all of them, if there are fewer than n. Never raises.

(Vec.take(#[1, 2, 3, 4], 2), Vec.take(#[1, 2], 10))
(#[1, 2], #[1, 2])
funcdrop
func drop :: Vec<'a>, Int -> Vec<'a>

Everything after the first n elements — empty if there are n or fewer. Never raises.

(Vec.drop(#[1, 2, 3, 4], 2), Vec.drop(#[1, 2], 10))
(#[3, 4], #[])
funcappend
func append :: Vec<'a>, Vec<'a> -> Vec<'a>

The elements of a followed by the elements of b.

Vec.append(#[1, 2], #[3, 4])
#[1, 2, 3, 4]
func reverse :: Vec<'a> -> Vec<'a>

The same elements, last first.

Vec.reverse(#[1, 2, 3])
#[3, 2, 1]
funcany
func any :: Vec<'a>, ('a -> Bool) -> Bool

true if p answers true for at least one element. false for an empty Vec.

(Vec.any(#[1, 3, 4], { x -> x % 2 == 0 }), Vec.any(#[1, 3, 5], { x -> x % 2 == 0 }))
(true, false)
funcall
func all :: Vec<'a>, ('a -> Bool) -> Bool

true if p answers true for every element. true for an empty Vec.

(Vec.all(#[2, 4], { x -> x % 2 == 0 }), Vec.all(#[2, 3], { x -> x % 2 == 0 }))
(true, false)
func find_index :: Vec<'a>, ('a -> Bool) -> Int

The index of the first element for which p answers true, or -1 if there is none. Vec.Opt.find_index answers an Option instead, so a miss cannot be mistaken for an index.

(Vec.find_index(#["a", "b", "c"], { s -> s == "b" }), Vec.find_index(#["a"], { s -> s == "z" }))
(1, -1)
funceach
func each :: Vec<'a>, ('a -> ()) -> ()

Calls f on every element, in order, for a callback whose answer is (). Because callbacks are pure, each is rarely what you want: use Vec.map to transform, Vec.fold to accumulate, and perform effects from the function that holds the Vec.

func flat_map :: Vec<'a>, ('a -> Vec<'b>) -> Vec<'b>

Applies f to every element and joins the Vecs it answers into one.

Vec.flat_map(#[1, 2, 3], { x -> #[x, x * 10] })
#[1, 10, 2, 20, 3, 30]
funcsort
func sort :: Vec<'a>, (('a, 'a) -> Bool) -> Vec<'a>

The elements in order, where lt(a, b) answers whether a belongs before b. The sort is stable: elements that lt cannot tell apart keep their original order.

let people = #[#{ name: "bo", age: 31 }, #{ name: "ada", age: 36 }, #{ name: "cy", age: 31 }];
let oldest_first = Vec.sort(people, { a, b -> a.age > b.age });
Vec.map(oldest_first, { p -> p.name })
#["ada", "bo", "cy"]