Array Methods
Method Reference
Section titled “Method Reference”| Method | Description |
|---|---|
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 |
Iterator Method Reference
Section titled “Iterator Method Reference”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.
Lazy (return iterator)
Section titled “Lazy (return iterator)”| Method | Description |
|---|---|
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 |
Eager (consume iterator)
Section titled “Eager (consume iterator)”| Method | Returns | Description |
|---|---|---|
next() | Value | Next element, or NUL when exhausted |
collect() | Array | Gather all remaining elements into an array |
count() | Number | Count remaining elements |
first() | Value | First element, or NUL if empty |
last() | Value | Last element, or NUL if empty |
find(fn) | Value | First element where fn returns true, or NUL |
any(fn) | Boolean | true if fn returns true for any element |
all(fn) | Boolean | true if fn returns true for every element |
fold(init, fn) | Value | Accumulate with fn(acc, elem) starting from init |
flatten() | Array | Flatten one level of nested arrays |
Examples
Section titled “Examples”let arr = Array(1, 2, 3);
// Mutation methodsarr.push(4); // arr = [1, 2, 3, 4]arr.pop(); // returns 4, arr = [1, 2, 3]
// Access methodsarr.length(); // 3arr.get(0); // 1arr.contains(2); // true
// Transformation methodsarr.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 methodsarr.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 methodsarr.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; }); // 2arr.iter().any(fn(x) { return x > 2; }); // truearr.iter().all(fn(x) { return x > 0; }); // truearr.iter().fold(0, fn(a, x) { return a + x; }); // 6
// Chaininglet 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]]See Also
Section titled “See Also”- Arrays - Array basics, creation, and iterators
- Built-in Functions -
Array(),length(),slice(),concat(),range()