sigildocs

(sigil fp chain)

(sigil fp chain) - Threading Macros

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

Basic Threading

(import (sigil fp chain))

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

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

Short-Circuit Threading

;; Returns #f if any step produces #f
(some-> user
        (dict-ref _ name:)
        (string-split " " _)
        car)

Exports

chainsyntax

Thread a value through a sequence of expressions.

Similar to SRFI-197's chain. Uses _ as the placeholder symbol. When a step contains _, the threaded value is substituted there. When no _ is present, the value is inserted as the first argument.

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

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

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

Alias for chain (Clojure-style threading operator).

(-> 5 (+ 3) (* 2))  ; => 16
some->syntax

Thread value through steps, short-circuiting on #f.

Like ->, but stops and returns #f if any step produces #f. Useful for optional/nullable value pipelines.

;; Returns #f if any step fails
(some-> user
        (dict-ref _ name:)
        (string-split " " _)
        car)

;; Short-circuits on #f
(some-> #f (+ 1 _))  ; => #f (doesn't call +)

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