RRB
RRB.Vec — persistent sequence for bulk-parallel work.
v2: backed by Array (32-way trie, O(1) amortised push, O(log₃₂ n) get).
A Slice is a zero-copy window into a Vec: Slice(arr, lo, hi). chunk(v, n) creates n Slices in O(n) — the underlying Array is shared. fold(s, z, f) iterates [lo, hi) using O(1)-amortised Array.get.
Complexity summary: empty, singleton, is_empty, length O(1) get, set O(log₃₂ n) push O(1) amortised concat(l, r) O(len(r) · log₃₂(len(l)+len(r))) slice(v, lo, hi) O(1) chunk(v, n) O(n) fold(slice, z, f) O((hi-lo) · log₃₂ n) from_list, to_list, tabulate, range O(n) map, fold_left, each O(n)
Examples
march> let v = RRB.from_list([1, 2, 3, 4, 5])
march> RRB.length(v)
5
march> let s = RRB.slice(v, 1, 4)
march> RRB.fold(s, 0, fn (acc, x) -> acc + x)
9Types
Functions
Split v into n roughly equal contiguous Slices in index order. Returns an Array of Slices. Each Slice is a zero-copy view (O(1) to create). O(n) total.
Edge cases:
- `n <= 0` is treated as 1.
- `n > length(v)` is clamped to `length(v)` (no empty slices).
- Empty `v` returns an empty Array.
## Examples
march> let v = RRB.range(0, 6)
march> Array.length(RRB.chunk(v, 3))
3Concatenate two Vecs: all elements of l followed by all of r. O(len(r) · log₃₂(len(l)+len(r))).
Examples
march> RRB.to_list(RRB.concat(RRB.from_list([1,2]), RRB.from_list([3,4])))
[1, 2, 3, 4]Left fold over a Slice in index order. f(acc, elem) = new_acc. O((hi-lo) · log₃₂ n).
This is the hot path inside each parallel worker: it visits only the
elements in the slice's range without copying or allocating.
## Examples
march> let s = RRB.slice(RRB.range(0, 5), 1, 4)
march> RRB.fold(s, 0, fn (acc, x) -> acc + x)
6Left fold over all elements. f(acc, elem) = new_acc. O(n).
Examples
march> RRB.fold_left(RRB.from_list([1, 2, 3]), 0, fn (acc, x) -> acc + x)
6Build a Vec from a list. O(n).
Return element at 0-based index i. O(log₃₂ n). Panics if out of bounds.
Examples
march> RRB.get(RRB.from_list([10, 20, 30]), 1)
20Apply f to every element, collecting results in a new Vec. O(n).
For parallel map, use `Parallel.pmap` instead — this is the sequential
version.Build a Vec of consecutive integers [lo, lo+1, …, hi-1]. O(hi-lo).
Examples
march> RRB.to_list(RRB.range(1, 5))
[1, 2, 3, 4]A Vec containing exactly one element. O(1).
Create a Slice covering the half-open range [lo, hi). O(1).
Indices are clamped to [0, length(v)]; lo > hi (after clamping) yields
an empty slice.
## Examples
march> RRB.fold(RRB.slice(RRB.range(0, 5), 1, 4), 0, fn (a, x) -> a + x)
6Build a Vec of length n by calling f(i) for each index i ∈ [0, n). O(n).
Examples
march> RRB.to_list(RRB.tabulate(4, fn i -> i * i))
[0, 1, 4, 9]