chain

2025-06-22

Two chaining/piping macros, one of them `setf`ing its first argument

Upstream URL

gitlab.com/Aksej/chain

Author

Thomas Bartscher <thomas-bartscher@weltraumschlangen.de>

License

BSD-3
README

Chain

[in package CHAIN]

  • [system] "chain"
    • Version: 0.0.1
    • Description: Two chaining/piping macros, one of them setfing its first argument
    • Licence: BSD-3
    • Author: Thomas Bartscher thomas-bartscher@weltraumschlangen.de
    • Depends on: metabang-bind, mgl-pax

  • [macro] => ARGUMENT &BODY BODY

    Thread a value through a series of transformations, where % represents the value of the last transformation.

    • Arguments

      • argument: Initial value or a (:var SYMBOL INITIAL-VALUE) binding form.

      • body: Forms to apply sequentially. Each form has % bound as an anaphoric variable bound to the return value of the previous form in the body. Each form may be:

        • A regular form

        • A (:var SYMBOL EXPR) form to bind SYMBOL to the result of evaluating

        EXPR

        • A (lambda (X) …) form that is called on the result of the previous form.
    • Return value

    Returns the value of its last form.

    • Examples
    ;; Basic numeric pipeline
    (=> 5
      (* % 2)  ; => 10
      (1+ %))
    => 11
    
    ;; lambda form usage
    (=> 5
      (lambda (x)  ; => 8
        (+ x 3))
      (* % 2))
    => 16
    
    ;; :var binding
    (=> 5
      (:var y (* % 3))  ; y = 15, % = 15
      (1+ %)            ; % = 16
      (+ y %))          ; y + % = 15 + 16
    => 31
    
    ;; :var in argument position
    (=> (:var a 5)  ; a = 5, % = 5
      (1+ %)        ; % = 6
      (+ a %))      ; a + % = 5 + 6
    => 11
    

  • [macro] SET=> ARGUMENT &BODY BODY

    Thread the initial value of a place through transformations like in macro =>, and update the place with the final result.

    • Arguments

      • argument: A setf-able place or (:var SYMBOL PLACE) where SYMBOL is a variable name and PLACE is a setf-able place. When refering to PLACE from here on out, the setf-able place is meant.

      • body: Exactly the same as in macro =>. Supports :var, lambda(0 1) and % the same as =>.

    • Return value

      Returns the value of its last form.

    • Side effects

      Updates PLACE to the calculated result.

    • Examples

    ;;; Modify a variable
    (let ((x 5))
      (set=> x
        (* % 2)  ; => 10
        (1+ %))  ; => 11
      x)
    => 11
    
    ;; Using :var
    (defstruct box (value 0))
    (let ((b (make-box :value 8)))
      (set=> (:var v (box-value b))
        (+ % 5)   ; => 13
        (* v %))  ; => 8 * 13 = 104
      (box-value b))
    => 104
    

Dependencies (2)

  • metabang-bind
  • mgl-pax

Dependents (0)

    • GitHub
    • Quicklisp