Rewrote --> to take a placeholder symbol as first argument

Instead of exporting cantedraw.lib.macro._ and making anyone who wants
to use the --> macro _require_ importing that specific symbol, let's
just make it so the user has to supply a placeholder name before they
do anything.  This means each package provides its own placeholder
symbol which lowers coupling.
This commit is contained in:
2025-02-14 16:34:15 +00:00
parent 02ff1a3fb3
commit f8a834c0d2
3 changed files with 15 additions and 14 deletions

View File

@@ -19,31 +19,31 @@
(in-package :cantedraw.lib.macros) (in-package :cantedraw.lib.macros)
(defmacro --> (&rest forms) (defmacro --> (placeholder &body forms)
"Lexically bind current form as `_' for use in the next form, returning the "Lexically bind current form as `placeholder' for use in the next form, returning
result of the last form. the result of the last form.
i.e. i.e.
(--> (a1 a2...) (b1 b2...) (c1 c2...)) = (--> (a1 a2...) (b1 b2...) (c1 c2...)) =
(let* ((_ (a1 a2 ...)) (let* ((placeholder (a1 a2 ...))
(_ (b1 b2 ...)) (placeholder (b1 b2 ...))
(_ (c1 c2 ...))) (placeholder (c1 c2 ...)))
_ ) _ )
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 placeholder)).
" "
(if (null forms) (if (null forms)
nil nil
(let ((assignment-forms (let ((assignment-forms
(loop :for f :in forms (loop :for f :in forms
:for canon-f := (if (symbolp f) :for canon-f := (if (symbolp f)
(list f 'cantedraw.lib.macros:_) (list f placeholder)
f) f)
:collect `(cantedraw.lib.macros:_ ,canon-f)))) :collect `(,placeholder ,canon-f))))
`(let* ,assignment-forms `(let* ,assignment-forms
cantedraw.lib.macros:_)))) ,placeholder))))
(defmacro ->> (&rest forms) (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

View File

@@ -44,6 +44,7 @@
inp)) inp))
(defun start () (defun start ()
(--> (read-until-integers) (--> var
(cons '+ _) (read-until-integers)
(format t "~a = ~a~%" _ (eval _)))) (cons '+ var)
(format t "~a = ~a~%" var (eval var))))

View File

@@ -20,7 +20,7 @@
(defpackage cantedraw.lib.macros (defpackage cantedraw.lib.macros
(:use :cl) (:use :cl)
(:export (:export
:_ :--> :->> :--> :->>
:-> :fn :-> :fn
:while :alist-val :while :alist-val
:$)) :$))