AtlatestRenderedmarkdown
Readme

sigil-fp

Functional programming utilities for Sigil, providing function composition, partial application, and threading macros.

Installation

Add to your package.sgl dependencies:

(from-git url: "codeberg:sigil/sigil-fp")

Modules

(sigil fp fn) - Function Composition

Utilities for composing and partially applying functions.

(import (sigil fp fn))

partial / partial-right

Create new functions by fixing arguments:

(define add10 (partial + 10))
(add10 5)      ; => 15
(add10 1 2 3)  ; => 16

(define div-by-2 (partial-right / 2))
(div-by-2 10)  ; => 5

compose

Right-to-left function composition:

(define process (compose string-upcase string-trim))
(process "  hello  ")  ; => "HELLO"

((compose) 42)  ; => 42 (identity)

pipe

Left-to-right function composition (pipeline style):

(define process (pipe string-trim string-upcase))
(process "  hello  ")  ; => "HELLO"

const

Return a function that always returns the given value:

(map (const 0) '(a b c))  ; => (0 0 0)

flip

Swap the first two arguments of a function:

((flip cons) '(1 2) 'a)  ; => (a 1 2)
((flip -) 3 10)           ; => 7

complement

Return the logical complement of a predicate:

(filter (complement zero?) '(0 1 0 2 0 3))  ; => (1 2 3)

juxt

Apply multiple functions to the same arguments, returning a list of results:

((juxt car cdr) '(1 2 3))  ; => (1 (2 3))
((juxt + - *) 3 4)          ; => (7 -1 12)

(sigil fp chain) - Threading Macros

Thread values through sequences of expressions, similar to Clojure's threading macros and SRFI-197.

These macros live in (sigil core) now. chain, ->, and some-> are part of core, which is auto-imported into every module, so every program already has them in scope without importing anything. (sigil fp chain) re-exports them from core purely for backward compatibility, so existing (import (sigil fp chain)) / (import (sigil fp)) code keeps working.
;; No import needed — these come from core:
(-> 5 (+ 3) (* 2))

;; Or, for backward compatibility:
(import (sigil fp chain))

chain / ->

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.

;; Thread-first (no placeholder)
(chain 5 (+ 3) (* 2))
; => 16

;; Explicit placeholder
(chain '(1 2 3)
       (map (lambda (x) (* x 2)) _)
       (apply + _))
; => 12

;; Placeholder in various positions
(chain 10 (- 20 _))          ; => 10
(chain 5 (list 1 2 _ 4))     ; => (1 2 5 4)

;; -> is an alias for chain
(-> 5 (+ 3) (* 2))  ; => 16

some->

Like ->, but short-circuits and returns #f if any step produces #f:

;; Short-circuits on #f
(some-> #f (+ 1 _))  ; => #f

;; Continues while truthy
(some-> 5 (+ 1 _) (* 2 _))  ; => 12

;; Useful for nullable pipelines
(some-> user
        (dict-ref _ name:)
        (string-split " " _)
        car)

Building

sigil deps install
sigil build
sigil test

License

BSD-3-Clause