# Sequence Functions
These are the higher-order verbs that walk any `ISeq` collection —
lists, sets, maps, strings, and lazy sequences alike. `map`, `filter`,
`take`, `drop`, and `mapcat` are arity-overloaded: the
sequence-first form returns a lazy sequence, while the 1-arg form
returns a transducer built for `comp`, `transduce`, and `into`. The
remaining functions are eager terminals. Semantics live in
Lazy Sequences and
Transducers; this page is the catalogue.
All functions are stdlib natives, available without an import.
## Functions
| Function | Purpose |
|---|---|
| `map xs f` / `map f` | Lazy seq / transducer — transform each element with `f` |
| `filter xs pred?` / `filter pred?` | Lazy seq / transducer — keep elements where `pred?` is truthy |
| `take xs n` / `take n` | Lazy seq / transducer — first `n` elements |
| `drop xs n` / `drop n` | Lazy seq / transducer — all but the first `n` |
| `mapcat xs f` / `mapcat f` | Lazy seq / transducer — apply `f`, concatenate the results |
| `reduce xs init f` | Folded value — accumulate with `($acc $it)` |
| `each xs f` | `nil` — side-effecting walk |
| `find xs pred?` | First match, or `nil` |
| `any? xs pred?` | Boolean — any element matches |
| `all? xs pred?` | Boolean — all elements match |
| `collect lazy-seq` | List — realise a lazy seq |
| `group xs f` | Map — partition by key `f` |
| `len xs` | Number — collection length, realising a lazy seq |
Notes:
- Two arities. For the five overloaded functions, passing the
sequence first (`[map $xs f]`, `$xs |> map f`, or `$xs(map f)`)
returns a lazy sequence: no work happens until a terminal operation
realises it. The 1-arg form (`[map f]`, `[take n]`) returns a
transducer, reusable across sources with `[into target xf source]` or
`[transduce xf rf init source]`.
- `reduce` calls `f` as `($acc $it)`; `each` is for side effects only
and returns `nil`; `map` is the one that returns values.
- `find`, `any?`, and `all?` short-circuit at the first decisive
element. `group` returns a map from each key to a list of members —
boolean key order is not guaranteed.
- Lazy results are not directly printable. Terminal operations
(`collect`, `reduce`, `each`, `len`) and implicit realisation in
`for`, `echo`, and `set` force the walk.
### Transducer Verbs
| Function | Purpose |
|---|---|
| `comp xf1 xf2 ...` | Compose transducers right-to-left; `[comp]` is the identity transducer |
| `transduce xf rf init coll` | Reduce a collection through a transducer |
| `into target xf coll` | Conjoin transformed elements into a vector, set, or map |
| `completing f ?cf` | Wrap a two-argument function as a `%Reducer` |
| `reduced v` | Wrap a value as a `%Reduced` early-termination signal |
| `reduced? v` | True when `v` is a `%Reduced` wrapper |
| `unreduced v` | Unwrap a `%Reduced` wrapper; idempotent on plain values |
The one-argument transducer forms of `map`, `filter`, `take`, `drop`,
and `mapcat` compose with `comp` and are applied through `transduce` or
`into`; Transducers covers the protocol
and the reduction machinery in full.
## Examples
# Lazy pipeline — only the first two even values are mapped @{ 1 2 3 4 5 6 } |> filter { ($it % 2) == 0 } |> map { ($it * 10) } |> take 2 |> collect # => @{ 20 40 } # The same functions as 1-arg transducers, composed and applied with into def xf [comp [map { ($it * 2) }] [filter { ($it > 3) }]] [into @{} $xf @{ 1 2 3 4 }] # => @{ 4 6 8 } # Eager terminals [reduce @{ 1 2 3 4 } 0 { ($acc + $it) }] # => 10 [find @{ 1 2 3 4 } { ($it > 2) }] # => 3 [any? @{ 1 2 3 } { ($it > 2) }] # => true [all? @{ 1 2 3 } { ($it > 0) }] # => true each @{ "a" "b" } { echo $it } # prints a, b; returns nil [group @{ 1 2 3 4 5 } { ($it % 2) == 0 }] # => %{ false @{1 3 5} true @{2 4} } [collect [map @{ 1 2 3 } { ($it * 2) }]] # => @{ 2 4 6 } [len [filter @{ 1 2 3 4 5 6 } { ($it % 2) == 0 }]] # => 3 [collect [mapcat @{ 1 2 3 } { @{ $it $it } }]] # => @{ 1 1 2 2 3 3 }
## See Also
- Lazy Sequences — the sequence-first arity
- Transducers — the 1-arg arity
- Lists — persistent list operations
- Maps — mapping over maps
- Sets — set operations and `into`