functions -> forms in lib.macros

This commit is contained in:
2025-02-10 07:13:16 +00:00
parent ef572b992b
commit c5d9888641

View File

@@ -19,7 +19,7 @@
(in-package :lib.macros) (in-package :lib.macros)
(defmacro --> (&rest functions) (defmacro --> (&rest forms)
"Lexically bind current form as `_' for use in the next form, returning the "Lexically bind current form as `_' for use in the next form, returning the
result of the last form. result of the last form.
@@ -34,30 +34,29 @@ i.e.
Also includes transformer where symbols are considered unary functions i.e. Also includes transformer where symbols are considered unary functions i.e.
(--> x y) <-> (--> x (y _)). (--> x y) <-> (--> x (y _)).
" "
(if (null functions) (if (null forms)
nil nil
(let ((assignment-forms (let ((assignment-forms
(loop :for f :in functions (loop :for f :in forms
:for canon-f = (if (symbolp f) (list f 'lib.macros:_) f) :for canon-f = (if (symbolp f) (list f 'lib.macros:_) f)
:collect `(lib.macros:_ ,canon-f)))) :collect `(lib.macros:_ ,canon-f))))
`(let* ,assignment-forms `(let* ,assignment-forms
lib.macros:_)))) lib.macros:_))))
(defmacro ->> (&rest functions) (defmacro ->> (&rest forms)
"Make current form the last argument of the next form, returning the last "Make current form the last argument of the next form, returning the last
form. form.
i.e. i.e.
(->> (a1 a2...) (b1 b2...) (c1 c2...)) == (c1 c2 ... (b1 b2 ... (a1 a2 ...))) (->> (a1 a2...) (b1 b2...) (c1 c2...)) == (c1 c2 ... (b1 b2 ... (a1 a2 ...)))
Also includes transformer where fun in functions that are symbols are considered Also includes transformer where symbols are considered unary functions.
unary functions.
Like the `|>' operator in Ocaml." Like the `|>' operator in Ocaml."
(if (null functions) (if (null forms)
nil nil
(loop :with acc = (car functions) (loop :with acc = (car forms)
:for func :in (cdr functions) :for func :in (cdr forms)
:for canon-func = (if (symbolp func) (list func) func) :for canon-func = (if (symbolp func) (list func) func)
:do (setq acc (append canon-func (list acc))) :do (setq acc (append canon-func (list acc)))
:finally (return acc)))) :finally (return acc))))