std.iter
std.iterπ
std.iter provides eager functional helpers over arrays. Array-in, array-out helpers such as map and filter allocate their result; a hand-written for loop can avoid that work when allocation matters.
This module is separate from Nyblβs built-in lazy .iter() / .next() protocol. You do not need use std.iter to write for value in collection, and the built-in protocol does not make these array helpers lazy.
Every helper handles empty arrays gracefully and preserves relative order.
Importπ
use std.iter // glob
use std.iter.{map, filter, reduce} // selective
use std.iter as i // aliasedHigher-orderπ
map(arr, f)π
Apply f to each element; return a new array of results.
use std.iter.{map}
print(map([1, 2, 3], fn(x) { return x * 2 })) // [2, 4, 6]filter(arr, pred)π
Keep only the elements for which pred(x) is truthy.
use std.iter.{filter}
let evens = filter([1, 2, 3, 4, 5], fn(n) { return n % 2 == 0 })
print(evens) // [2, 4]reduce(arr, initial, combine)π
Fold over the array left-to-right with a two-arg combiner.
use std.iter.{reduce}
let sum = reduce([1, 2, 3, 4], 0, fn(acc, x) { return acc + x })
print(sum) // 10all(arr, pred), any(arr, pred)π
all returns true when every element passes (vacuously true for empty). any returns true when at least one element passes.
use std.iter.{all, any}
print(all([2, 4, 6], fn(n) { return n % 2 == 0 })) // true
print(any([1, 3, 4], fn(n) { return n % 2 == 0 })) // truecount(arr, pred)π
Count elements for which pred(x) is truthy.
use std.iter.{count}
print(count([1, 2, 3, 4, 5], fn(n) { return n > 2 })) // 3find(arr, pred) / find_index(arr, pred)π
find returns the first matching element, or none if no match. find_index returns the 0-based index, or -1.
use std.iter.{find, find_index}
print(find([1, 2, 3, 4], fn(n) { return n > 2 })) // 3
print(find_index([1, 2, 3, 4], fn(n) { return n > 2 })) // 2Slicingπ
take(arr, n)π
First n elements (or the whole array if itβs shorter). Negative n yields an empty array.
use std.iter.{take}
print(take([1, 2, 3, 4, 5], 3)) // [1, 2, 3]
print(take([1, 2], 10)) // [1, 2]drop(arr, n)π
Drop the first n elements. Negative n yields a full copy; n >= arr.len() yields [].
use std.iter.{drop}
print(drop([1, 2, 3, 4, 5], 2)) // [3, 4, 5]Combiningπ
zip(a, b)π
Pair elements from two arrays. Stops at the shorter arrayβs length.
use std.iter.{zip}
print(zip([1, 2, 3], ["a", "b", "c"])) // [[1, "a"], [2, "b"], [3, "c"]]enumerate(arr)π
Pair each element with its 0-based index.
use std.iter.{enumerate}
print(enumerate(["a", "b"])) // [[0, "a"], [1, "b"]]flatten(arr)π
Flatten an array of arrays one level down.
use std.iter.{flatten}
print(flatten([[1, 2], [3, 4], [5]])) // [1, 2, 3, 4, 5]Reductionsπ
sum(arr)π
Sum of a numeric array. Empty β 0.
use std.iter.{sum}
print(sum([1, 2, 3, 4])) // 10
print(sum([])) // 0product(arr)π
Product of a numeric array. Empty β 1 (multiplicative identity, matching NumPy / Pythonβs math.prod).
use std.iter.{product}
print(product([2, 3, 4])) // 24
print(product([])) // 1min_array(arr) / max_array(arr)π
Minimum / maximum of a numeric array. Raises on empty input so callers notice.
use std.iter.{min_array, max_array}
print(min_array([3, 1, 4, 1, 5])) // 1
print(max_array([3, 1, 4, 1, 5])) // 5For pairwise min / max use the .min() / .max() numeric methods instead.