diff options
author | Colin Okay <cbeok@protonmail.com> | 2020-07-14 10:21:32 -0500 |
---|---|---|
committer | Colin Okay <cbeok@protonmail.com> | 2020-07-14 10:21:32 -0500 |
commit | cc03f9b4985d802661cc9e1c8778188522f0ed6e (patch) | |
tree | 96ab2e7c889b787df089253e270297ce7ba6c575 | |
parent | dfd02e7d038f043d079dce1d55c750f941775fea (diff) |
added no nesting check, changed name of $ to $$
-rw-r--r-- | macros.lisp | 31 | ||||
-rw-r--r-- | package.lisp | 2 |
2 files changed, 21 insertions, 12 deletions
diff --git a/macros.lisp b/macros.lisp index cc8a54c..c354a89 100644 --- a/macros.lisp +++ b/macros.lisp @@ -2,7 +2,8 @@ (eval-when (:compile-toplevel :load-toplevel :execute) (defun is-substitute-var (symbol) - (and (symbolp symbol) + (and (not (eql '$$ symbol)) + (symbolp symbol) (eq (elt (symbol-name symbol) 0) #\$)))) @@ -10,8 +11,8 @@ -(defmacro $ (expr) -"Quickly create functions from an expression EXPR with 'blanks' in +(defmacro $$ (expr) + "Quickly create functions from an expression EXPR with 'blanks' in it. Each blank is a symbol that betins with the underscore _. Symbols with the same name are treated as the same variable. @@ -19,7 +20,10 @@ A function is returned, it accepts exactly the number of variables as there were unique blanks in the expression. When calling the new function, the variables are bound in the order they appeared in EXPR. -> (macroexpand-1 '($ (+ $a (* $b 3) $b (- $a $c) 10))) +This is a rather simple macro - you cannot nest $$ forms. If you try, +an error will be raised. + +> (macroexpand-1 '($$ (+ $a (* $b 3) $b (- $a $c) 10))) (LAMBDA ($A $B $C) (+ $A (* $B 3) @@ -30,7 +34,7 @@ function, the variables are bound in the order they appeared in EXPR. The macro is useful for succinctly passing functions to higher order functions: -> (mapcar ($ (+ $ 10)) '(1 2 3 4)) +> (mapcar ($$ (+ $ 10)) '(1 2 3 4)) (11 12 13 14) > (let ((elt-num 2)) @@ -38,13 +42,18 @@ higher order functions: (list \"hey dude\" #(1 2 3 4) \"ffffffffff\"))) - (#\y 3 #\f) -" - (let ((new-params (list))) + (#\y 3 #\f)" + (let ((new-params (list)) + (calls-to-$$ 0)) (subst-if t (constantly nil) expr - :key (lambda (x) (when (is-substitute-var x) - (pushnew x new-params)))) - `(lambda ,(reverse new-params) ,expr))) + :key (lambda (x) + (when (is-substitute-var x) + (pushnew x new-params)) + (when (eql '$$ x) + (incf calls-to-$$)))) + (if (not (zerop calls-to-$$)) + (error "$$ cannot be nested") + `(lambda ,(reverse new-params) ,expr)))) diff --git a/package.lisp b/package.lisp index d037ff1..3160113 100644 --- a/package.lisp +++ b/package.lisp @@ -4,6 +4,6 @@ (:use #:cl) (:import-from #:alexandria #:if-let) - (:export #:$ + (:export #:$$ #:$and #:$or)) |