/iter
Lazy iterator combinators and reductions (map, filter, fold, reduce, zip, take, skip, chunk, flatten, group-by, partition).
Functions & symbols#
Iter#
Wraps an `Array` in an explicit stateful cursor iterator.
| Parameter | Type | Optional | Description |
|---|---|---|---|
arr | (Array Any) | required | Backing Array to iterate |
Any(let it (iter/Iter [10 20 30]))all?#
Returns `true` if every item in `xs` satisfies `pred`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
xs | Array | Iter | required | Sequence to check |
f | Func | required | Predicate `(Func item -> Bool)` |
Any(iter/all? [2 4 6] (Func x (== (% x 2) 0))) ; -> trueany?#
Returns `true` if any item in `xs` satisfies `pred`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
xs | Array | Iter | required | Sequence to check |
f | Func | required | Predicate `(Func item -> Bool)` |
Any(iter/any? [1 3 4] (Func x (== (% x 2) 0))) ; -> truechunks#
Splits `xs` into non-overlapping sub-Arrays of up to `size` items.
| Parameter | Type | Optional | Description |
|---|---|---|---|
xs | Array | required | Sequence to chunk |
size | Int | required | Maximum items per chunk > 0 |
Any(iter/chunks [1 2 3 4 5] 2) ; -> [[1 2] [3 4] [5]]compact#
Filters out all `()` (`None`) elements from `xs`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
xs | Array | required | Sequence possibly containing `()` (`None`) |
Any(iter/compact [1 () 2 () 3]) ; -> [1 2 3]drop#
Returns collection excluding the first `n` items as an Array.
| Parameter | Type | Optional | Description |
|---|---|---|---|
coll | Array | Iter | Range | required | Source collection |
n | Int | required | Count to skip |
Any(iter/drop [1 2 3 4] 2) ; -> [3 4]filter#
Returns an Array of elements for which `predicate` returns `true`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
coll | Array | Iter | Range | required | Source collection |
predicate | Func | required | Predicate function (item -> Bool) |
Any(iter/filter [1 2 3 4] (Func x (== (% x 2) 0))) ; -> [2 4]filter-unique#
Deduplicates elements of collection in O(N) time while preserving first-seen insertion order.
| Parameter | Type | Optional | Description |
|---|---|---|---|
coll | Array | Iter | required | Source collection |
Any(use {filter-unique} /iter)
(filter-unique ["b" "a" "b" "c"]) ; -> ["b" "a" "c"]filter-unique-by#
Deduplicates elements of collection by `(key-fn item)` in O(N) time while preserving first-seen insertion order.
| Parameter | Type | Optional | Description |
|---|---|---|---|
coll | Array | Iter | required | Source collection |
key-fn | Func | required | Key extraction function (item -> key) |
Any(use {filter-unique-by} /iter)
(filter-unique-by users (Func u u:id))find#
Returns the first element satisfying `predicate`, or `()` if none found.
| Parameter | Type | Optional | Description |
|---|---|---|---|
xs | Array | Iter | Range | required | Source collection |
f | Func | required | Condition |
Any(iter/find [1 2 3] (Func x (> x 1))) ; -> 2find-index#
Returns the 0-based index of the first element matching `pred`, or `()`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
xs | Array | required | Sequence |
pred | Func | required | Predicate `(Func item -> Bool)` |
Any(iter/find-index [10 25 30] (Func x (> x 20))) ; -> 1flat-map#
Maps `func` over each element and flattens one level into a single `Array`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
xs | Array | required | Source sequence |
func | Func | required | Mapping function returning an Array per element |
Any(iter/flat-map [1 2] (Func x [x (* x 10)])) ; -> [1 10 2 20]flatten#
Recursively flattens nested `Array`s into a single flat `Array`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
xs | Array | required | Nested Array structure |
Any(iter/flatten [[1 [2]] [3 4]]) ; -> [1 2 3 4]fold#
Folds collection left-to-right with accumulator and reducer function.
| Parameter | Type | Optional | Description |
|---|---|---|---|
coll | Array | Iter | Range | required | Source collection |
init | Any | required | Initial accumulator value |
func | Func | required | Reducer (acc, item -> new_acc) |
Any(iter/fold [1 2 3] 0 (Func [acc x] (+ acc x))) ; -> 6group-by#
Groups elements of `xs` into a `Map` of `{key [matching-items ...]}`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
xs | Array | required | Source sequence |
key_fn | Func | required | Grouping key function `(Func item -> key)` |
Any(iter/group-by ["ant" "bear" "cat"] count-of)map#
Applies `func` to each item in collection, returning a new Array of results.
| Parameter | Type | Optional | Description |
|---|---|---|---|
coll | Array | Iter | Range | required | Source collection |
func | Func | required | Transform function (item -> new_item) |
Any(iter/map [1 2 3] (Func x (* x 2))) ; -> [2 4 6]next#
Advances the cursor `it` and returns the current element.
| Parameter | Type | Optional | Description |
|---|---|---|---|
it | Iter | required | Iterator cursor |
Any(iter/next it)partition#
Splits `xs` in a single pass into `[matched unmatched]` Arrays.
| Parameter | Type | Optional | Description |
|---|---|---|---|
xs | Array | required | Source sequence |
pred | Func | required | Predicate `(Func item -> Bool)` |
Any(iter/partition [1 2 3 4] (Func x (== (% x 2) 0))) ; -> [[2 4] [1 3]]reduce#
Reduces a sequence left-to-right using its first element as initial accumulator.
| Parameter | Type | Optional | Description |
|---|---|---|---|
xs | Array | required | Non-empty sequence |
func | Func | required | Accumulator `(Func [acc x] ...)` |
Any(iter/reduce [1 2 3 4] (Func [acc x] (+ acc x))) ; -> 10rewind#
Moves an `Iter` cursor backward.
| Parameter | Type | Optional | Description |
|---|---|---|---|
it | Iter | required | Iterator cursor |
step | Int | required | Step delta |
Any(iter/rewind it 1)take#
Returns the first `n` items from collection as an Array.
| Parameter | Type | Optional | Description |
|---|---|---|---|
coll | Array | Iter | Range | required | Source collection |
n | Int | required | Count to take |
Any(iter/take [1 2 3 4] 2) ; -> [1 2]windows#
Returns overlapping sliding sub-Arrays of exact length `size`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
xs | Array | required | Sequence |
size | Int | required | Sliding window width > 0 |
Any(iter/windows [1 2 3 4] 2) ; -> [[1 2] [2 3] [3 4]]