AtlatestRepositorysigil-transducers
sigil-transducers / treeREADME.md
1
# sigil-transducers3
Composable transducer library for Sigil. Provides polymorphic sequence operations and transducer pipelines that work across lists, vectors, arrays, and dicts.5
## Installation7
Add to your `package.sgl` dependencies:9
```scheme10
(from-git url: "codeberg:sigil/sigil-transducers")11
```13
## Usage15
### Polymorphic Operations17
When imported, `(sigil seq)` provides polymorphic versions of common operations that work on any collection type:19
```scheme20
(import (sigil seq))22
(map square '(1 2 3)) ; => (1 4 9)23
(map square #(1 2 3)) ; => #(1 4 9)24
(filter even? '(1 2 3 4)) ; => (2 4)25
(filter even? #(1 2 3 4)) ; => #(2 4)26
(fold + 0 '(1 2 3 4)) ; => 1027
(any even? '(1 3 4 5)) ; => #t28
(every even? '(2 4 6)) ; => #t29
(find even? '(1 3 4 5)) ; => 430
```32
### Transducers34
Transducers are composable transformation pipelines that separate *what* to transform from *how* to iterate:36
```scheme37
(import (sigil seq))39
(define xform40
(comp (filtering even?)41
(mapping square)42
(taking 5)))44
(transduce xform conj '() (iota 100)) ; => (0 4 16 36 64)45
```47
### Transducer Constructors49
- `(mapping f)` - Transform each element by applying `f`50
- `(filtering pred)` - Keep only elements satisfying `pred`51
- `(taking n)` - Take at most `n` elements, then stop52
- `(dropping n)` - Skip the first `n` elements53
- `(taking-while pred)` - Take elements while `pred` is true54
- `(dropping-while pred)` - Drop elements while `pred` is true55
- `cat` - Flatten one level of nesting56
- `(mapcat f)` - Map then concatenate58
### Composition60
Compose transducers left-to-right with `comp`:62
```scheme63
(comp (filtering even?) (mapping square) (taking 3))64
```66
### Execution68
- `(transduce xform rf init coll)` - Apply transducer `xform` with reducing function `rf`, initial value `init`, over collection `coll`69
- `(into to xform from)` - Transform and collect into a collection of the same type as `to`70
- `(sequence xform coll)` - Transform a collection, preserving its type72
### Reducers74
- `conj` - Build lists (via cons)75
- `conj-vec` - Build vectors (builds list, convert with `into`)76
- `conj-dict` - Build dicts from `(key . value)` pairs78
### Stream Processing80
Transducers also work for processing channel message streams:82
```scheme83
(import (sigil seq)84
(sigil channels)85
(sigil async))87
(define log-xform88
(comp (filtering (lambda (msg) (equal? (dict-ref msg level:) "error")))89
(mapping (lambda (msg) (dict-ref msg text:)))))91
(with-async92
(go (for-each (lambda (msg) (channel-send logs msg)) messages)93
(channel-close! logs))94
(go (for-channel (msg logs)95
(let ((xrf (log-xform (lambda (acc x) (println "ERROR: ~a" x) acc))))96
(xrf #f msg)))))97
```99
## License101
BSD-3-Clause