Skip to content

Array Methods

MethodDescription
length()Get array length
push(val)Add element to end (mutates in-place)
pop()Remove and return last element (mutates)
get(index)Get element by index
slice(start, end)Get subarray [start, end)
concat(other)Concatenate with another array
reverse()Return reversed array
contains(value)Check if value exists
map(func)Apply function to each element
filter(func)Keep elements matching predicate
reduce(func, init)Reduce to single value
iter()Create iterator over elements

Call .iter() on an array to get an iterator, then use these methods. Lazy methods return a new iterator; eager methods consume the iterator and return a value or array.

MethodDescription
take(n)Yield the first n elements
skip(n)Skip the first n elements
step_by(n)Yield every nth element (must be > 0)
enumerate()Yield [index, value] pairs
zip(iter)Pair elements from two iterators (stops at shorter)
chain(iter)Concatenate two iterators
map(fn)Apply fn to each element
filter(fn)Keep elements where fn returns true
MethodReturnsDescription
next()ValueNext element, or NUL when exhausted
collect()ArrayGather all remaining elements into an array
count()NumberCount remaining elements
first()ValueFirst element, or NUL if empty
last()ValueLast element, or NUL if empty
find(fn)ValueFirst element where fn returns true, or NUL
any(fn)Booleantrue if fn returns true for any element
all(fn)Booleantrue if fn returns true for every element
fold(init, fn)ValueAccumulate with fn(acc, elem) starting from init
flatten()ArrayFlatten one level of nested arrays
let arr = Array(1, 2, 3);
// Mutation methods
arr.push(4); // arr = [1, 2, 3, 4]
arr.pop(); // returns 4, arr = [1, 2, 3]
// Access methods
arr.length(); // 3
arr.get(0); // 1
arr.contains(2); // true
// Transformation methods
arr.slice(1, 3); // Array(2, 3)
arr.concat(Array(4, 5)); // Array(1, 2, 3, 4, 5)
arr.reverse(); // Array(3, 2, 1)
// Higher-order methods
arr.map(fn(x) { return x * 2; }); // Array(2, 4, 6)
arr.filter(fn(x) { return x > 1; }); // Array(2, 3)
arr.reduce(fn(a, x) { return a + x; }, 0); // 6
// Iterator methods
arr.iter().take(2).collect(); // [1, 2]
arr.iter().skip(1).collect(); // [2, 3]
arr.iter().enumerate().collect(); // [[0, 1], [1, 2], [2, 3]]
arr.iter().find(fn(x) { return x > 1; }); // 2
arr.iter().any(fn(x) { return x > 2; }); // true
arr.iter().all(fn(x) { return x > 0; }); // true
arr.iter().fold(0, fn(a, x) { return a + x; }); // 6
// Chaining
let a = Array(1, 2, 3);
let b = Array(4, 5, 6);
a.iter().chain(b.iter()).step_by(2).collect(); // [1, 3, 5]
a.iter().zip(b.iter()).collect(); // [[1, 4], [2, 5], [3, 6]]