AtlatestRepositorysigil-fp
1# sigil-fp
2
3> **Archived at Sigil 0.19.0:** The maintained implementation now lives in
4> `codeberg:sigil/sigil`. This repository and all tags remain available for
5> provenance, but no further standalone releases will be published.
6
7Functional programming utilities for [Sigil](https://codeberg.org/sigil/sigil), providing function composition, partial application, and threading macros.
8
9## Installation
11Add to your `package.sgl` dependencies:
13```scheme
14(from-git url: "codeberg:sigil/sigil-fp")
15```
17## Modules
19### (sigil fp fn) - Function Composition
21Utilities for composing and partially applying functions.
23```scheme
24(import (sigil fp fn))
25```
27#### partial / partial-right
29Create new functions by fixing arguments:
31```scheme
32(define add10 (partial + 10))
33(add10 5) ; => 15
34(add10 1 2 3) ; => 16
36(define div-by-2 (partial-right / 2))
37(div-by-2 10) ; => 5
38```
40#### compose
42Right-to-left function composition:
44```scheme
45(define process (compose string-upcase string-trim))
46(process " hello ") ; => "HELLO"
48((compose) 42) ; => 42 (identity)
49```
51#### pipe
53Left-to-right function composition (pipeline style):
55```scheme
56(define process (pipe string-trim string-upcase))
57(process " hello ") ; => "HELLO"
58```
60#### const
62Return a function that always returns the given value:
64```scheme
65(map (const 0) '(a b c)) ; => (0 0 0)
66```
68#### flip
70Swap the first two arguments of a function:
72```scheme
73((flip cons) '(1 2) 'a) ; => (a 1 2)
74((flip -) 3 10) ; => 7
75```
77#### complement
79Return the logical complement of a predicate:
81```scheme
82(filter (complement zero?) '(0 1 0 2 0 3)) ; => (1 2 3)
83```
85#### juxt
87Apply multiple functions to the same arguments, returning a list of results:
89```scheme
90((juxt car cdr) '(1 2 3)) ; => (1 (2 3))
91((juxt + - *) 3 4) ; => (7 -1 12)
92```
94### (sigil fp chain) - Threading Macros
96Thread values through sequences of expressions, similar to Clojure's threading macros and SRFI-197.
98> **These macros live in `(sigil core)` now.** `chain`, `->`, and `some->` are
99> part of core, which is auto-imported into every module, so every program
100> already has them in scope without importing anything. `(sigil fp chain)`
101> re-exports them from core purely for backward compatibility, so existing
102> `(import (sigil fp chain))` / `(import (sigil fp))` code keeps working.
104```scheme
105;; No import needed — these come from core:
106(-> 5 (+ 3) (* 2))
108;; Or, for backward compatibility:
109(import (sigil fp chain))
110```
112#### chain / ->
114Thread a value through expressions. Uses `_` as a placeholder for the threaded value. When no `_` is present, the value is inserted as the first argument.
116```scheme
117;; Thread-first (no placeholder)
118(chain 5 (+ 3) (* 2))
119; => 16
121;; Explicit placeholder
122(chain '(1 2 3)
123 (map (lambda (x) (* x 2)) _)
124 (apply + _))
125; => 12
127;; Placeholder in various positions
128(chain 10 (- 20 _)) ; => 10
129(chain 5 (list 1 2 _ 4)) ; => (1 2 5 4)
131;; -> is an alias for chain
132(-> 5 (+ 3) (* 2)) ; => 16
133```
135#### some->
137Like `->`, but short-circuits and returns `#f` if any step produces `#f`:
139```scheme
140;; Short-circuits on #f
141(some-> #f (+ 1 _)) ; => #f
143;; Continues while truthy
144(some-> 5 (+ 1 _) (* 2 _)) ; => 12
146;; Useful for nullable pipelines
147(some-> user
148 (dict-ref _ name:)
149 (string-split " " _)
150 car)
151```
153## Building
155```
156sigil deps install
157sigil build
158sigil test
159```
161## License
163BSD-3-Clause