# Module /iter

Lazy iterator combinators and reductions (map, filter, fold, reduce, zip, take, skip, chunk, flatten, group-by, partition).

```tangu
(use /iter)
```

## Functions & symbols

### Iter

`(iter/Iter arr) -> Iter`

Wraps an `Array` in an explicit stateful cursor iterator.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `arr` | `(Array Any)` | no | Backing Array to iterate |

```tangu
(let it (iter/Iter [10 20 30]))
```

### all?

`(iter/all? xs pred) -> Bool`

Returns `true` if every item in `xs` satisfies `pred`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `xs` | `Array | Iter` | no | Sequence to check |
| `f` | `Func` | no | Predicate `(Func item -> Bool)` |

```tangu
(iter/all? [2 4 6] (Func x (== (% x 2) 0))) ; -> true
```

### any?

`(iter/any? xs pred) -> Bool`

Returns `true` if any item in `xs` satisfies `pred`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `xs` | `Array | Iter` | no | Sequence to check |
| `f` | `Func` | no | Predicate `(Func item -> Bool)` |

```tangu
(iter/any? [1 3 4] (Func x (== (% x 2) 0))) ; -> true
```

### chunks

`(iter/chunks xs size) -> (Array Array)`

Splits `xs` into non-overlapping sub-Arrays of up to `size` items.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `xs` | `Array` | no | Sequence to chunk |
| `size` | `Int` | no | Maximum items per chunk > 0 |

```tangu
(iter/chunks [1 2 3 4 5] 2) ; -> [[1 2] [3 4] [5]]
```

### compact

`(iter/compact xs) -> Array`

Filters out all `()` (`None`) elements from `xs`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `xs` | `Array` | no | Sequence possibly containing `()` (`None`) |

```tangu
(iter/compact [1 () 2 () 3]) ; -> [1 2 3]
```

### drop

`(iter/drop coll n) -> Array`

Returns collection excluding the first `n` items as an Array.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `coll` | `Array | Iter | Range` | no | Source collection |
| `n` | `Int` | no | Count to skip |

```tangu
(iter/drop [1 2 3 4] 2) ; -> [3 4]
```

### filter

`(iter/filter coll predicate) -> Array`

Returns an Array of elements for which `predicate` returns `true`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `coll` | `Array | Iter | Range` | no | Source collection |
| `predicate` | `Func` | no | Predicate function (item -> Bool) |

```tangu
(iter/filter [1 2 3 4] (Func x (== (% x 2) 0))) ; -> [2 4]
```

### filter-unique

`(iter/filter-unique coll) -> Array`

Deduplicates elements of collection in O(N) time while preserving first-seen insertion order.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `coll` | `Array | Iter` | no | Source collection |

```tangu
(use {filter-unique} /iter)
(filter-unique ["b" "a" "b" "c"]) ; -> ["b" "a" "c"]
```

### filter-unique-by

`(iter/filter-unique-by coll key-fn) -> Array`

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` | no | Source collection |
| `key-fn` | `Func` | no | Key extraction function (item -> key) |

```tangu
(use {filter-unique-by} /iter)
(filter-unique-by users (Func u u:id))
```

### find

`(iter/find coll predicate) -> (Or Any)`

Returns the first element satisfying `predicate`, or `()` if none found.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `xs` | `Array | Iter | Range` | no | Source collection |
| `f` | `Func` | no | Condition |

```tangu
(iter/find [1 2 3] (Func x (> x 1))) ; -> 2
```

### find-index

`(iter/find-index xs pred) -> (Or Int None)`

Returns the 0-based index of the first element matching `pred`, or `()`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `xs` | `Array` | no | Sequence |
| `pred` | `Func` | no | Predicate `(Func item -> Bool)` |

```tangu
(iter/find-index [10 25 30] (Func x (> x 20))) ; -> 1
```

### flat-map

`(iter/flat-map xs func) -> Array`

Maps `func` over each element and flattens one level into a single `Array`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `xs` | `Array` | no | Source sequence |
| `func` | `Func` | no | Mapping function returning an Array per element |

```tangu
(iter/flat-map [1 2] (Func x [x (* x 10)])) ; -> [1 10 2 20]
```

### flatten

`(iter/flatten xs) -> Array`

Recursively flattens nested `Array`s into a single flat `Array`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `xs` | `Array` | no | Nested Array structure |

```tangu
(iter/flatten [[1 [2]] [3 4]]) ; -> [1 2 3 4]
```

### fold

`(iter/fold coll init func) -> Any`

Folds collection left-to-right with accumulator and reducer function.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `coll` | `Array | Iter | Range` | no | Source collection |
| `init` | `Any` | no | Initial accumulator value |
| `func` | `Func` | no | Reducer (acc, item -> new_acc) |

```tangu
(iter/fold [1 2 3] 0 (Func [acc x] (+ acc x))) ; -> 6
```

### group-by

`(iter/group-by xs key-fn) -> Map`

Groups elements of `xs` into a `Map` of `{key [matching-items ...]}`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `xs` | `Array` | no | Source sequence |
| `key_fn` | `Func` | no | Grouping key function `(Func item -> key)` |

```tangu
(iter/group-by ["ant" "bear" "cat"] count-of)
```

### map

`(iter/map coll func) -> Array`

Applies `func` to each item in collection, returning a new Array of results.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `coll` | `Array | Iter | Range` | no | Source collection |
| `func` | `Func` | no | Transform function (item -> new_item) |

```tangu
(iter/map [1 2 3] (Func x (* x 2))) ; -> [2 4 6]
```

### next

`(iter/next it) -> Any`

Advances the cursor `it` and returns the current element.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `it` | `Iter` | no | Iterator cursor |

```tangu
(iter/next it)
```

### partition

`(iter/partition xs pred) -> Array`

Splits `xs` in a single pass into `[matched unmatched]` Arrays.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `xs` | `Array` | no | Source sequence |
| `pred` | `Func` | no | Predicate `(Func item -> Bool)` |

```tangu
(iter/partition [1 2 3 4] (Func x (== (% x 2) 0))) ; -> [[2 4] [1 3]]
```

### reduce

`(iter/reduce xs func) -> Any`

Reduces a sequence left-to-right using its first element as initial accumulator.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `xs` | `Array` | no | Non-empty sequence |
| `func` | `Func` | no | Accumulator `(Func [acc x] ...)` |

```tangu
(iter/reduce [1 2 3 4] (Func [acc x] (+ acc x))) ; -> 10
```

### rewind

`(iter/rewind it step) -> Any`

Moves an `Iter` cursor backward.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `it` | `Iter` | no | Iterator cursor |
| `step` | `Int` | no | Step delta |

```tangu
(iter/rewind it 1)
```

### take

`(iter/take coll n) -> Array`

Returns the first `n` items from collection as an Array.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `coll` | `Array | Iter | Range` | no | Source collection |
| `n` | `Int` | no | Count to take |

```tangu
(iter/take [1 2 3 4] 2) ; -> [1 2]
```

### windows

`(iter/windows xs size) -> (Array Array)`

Returns overlapping sliding sub-Arrays of exact length `size`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `xs` | `Array` | no | Sequence |
| `size` | `Int` | no | Sliding window width > 0 |

```tangu
(iter/windows [1 2 3 4] 2) ; -> [[1 2] [2 3] [3 4]]
```
