AtlatestRepositorysigil-fp
1
# sigil-fp3
Functional programming utilities for [Sigil](https://codeberg.org/sigil/sigil), providing function composition, partial application, and threading macros.5
## Installation7
Add to your `package.sgl` dependencies:9
```scheme10
(from-git url: "codeberg:sigil/sigil-fp")11
```13
## Modules15
### (sigil fp fn) - Function Composition17
Utilities for composing and partially applying functions.19
```scheme20
(import (sigil fp fn))21
```23
#### partial / partial-right25
Create new functions by fixing arguments:27
```scheme28
(define add10 (partial + 10))29
(add10 5) ; => 1530
(add10 1 2 3) ; => 1632
(define div-by-2 (partial-right / 2))33
(div-by-2 10) ; => 534
```36
#### compose38
Right-to-left function composition:40
```scheme41
(define process (compose string-upcase string-trim))42
(process " hello ") ; => "HELLO"44
((compose) 42) ; => 42 (identity)45
```47
#### pipe49
Left-to-right function composition (pipeline style):51
```scheme52
(define process (pipe string-trim string-upcase))53
(process " hello ") ; => "HELLO"54
```56
#### const58
Return a function that always returns the given value:60
```scheme61
(map (const 0) '(a b c)) ; => (0 0 0)62
```64
#### flip66
Swap the first two arguments of a function:68
```scheme69
((flip cons) '(1 2) 'a) ; => (a 1 2)70
((flip -) 3 10) ; => 771
```73
#### complement75
Return the logical complement of a predicate:77
```scheme78
(filter (complement zero?) '(0 1 0 2 0 3)) ; => (1 2 3)79
```81
#### juxt83
Apply multiple functions to the same arguments, returning a list of results:85
```scheme86
((juxt car cdr) '(1 2 3)) ; => (1 (2 3))87
((juxt + - *) 3 4) ; => (7 -1 12)88
```90
### (sigil fp chain) - Threading Macros92
Thread values through sequences of expressions, similar to Clojure's threading macros and SRFI-197.94
> **These macros live in `(sigil core)` now.** `chain`, `->`, and `some->` are95
> part of core, which is auto-imported into every module, so every program96
> already has them in scope without importing anything. `(sigil fp chain)`97
> re-exports them from core purely for backward compatibility, so existing98
> `(import (sigil fp chain))` / `(import (sigil fp))` code keeps working.100
```scheme101
;; No import needed — these come from core:102
(-> 5 (+ 3) (* 2))104
;; Or, for backward compatibility:105
(import (sigil fp chain))106
```108
#### chain / ->110
Thread a value through expressions. Uses `_` as a placeholder for the threaded value. When no `_` is present, the value is inserted as the first argument.112
```scheme113
;; Thread-first (no placeholder)114
(chain 5 (+ 3) (* 2))115
; => 16117
;; Explicit placeholder118
(chain '(1 2 3)119
(map (lambda (x) (* x 2)) _)120
(apply + _))121
; => 12123
;; Placeholder in various positions124
(chain 10 (- 20 _)) ; => 10125
(chain 5 (list 1 2 _ 4)) ; => (1 2 5 4)127
;; -> is an alias for chain128
(-> 5 (+ 3) (* 2)) ; => 16129
```131
#### some->133
Like `->`, but short-circuits and returns `#f` if any step produces `#f`:135
```scheme136
;; Short-circuits on #f137
(some-> #f (+ 1 _)) ; => #f139
;; Continues while truthy140
(some-> 5 (+ 1 _) (* 2 _)) ; => 12142
;; Useful for nullable pipelines143
(some-> user144
(dict-ref _ name:)145
(string-split " " _)146
car)147
```149
## Building151
```152
sigil deps install153
sigil build154
sigil test155
```157
## License159
BSD-3-Clause