Created
November 16, 2010 08:00
-
-
Save kurohuku/701572 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro with-collects ((&rest syms) &body body) | |
(labels | |
((collector-expander (sym gtail) | |
`(,sym (arg) | |
(if (null ,sym) | |
(progn | |
(setf ,sym (cons arg nil)) | |
(setf ,gtail ,sym) | |
arg) | |
(let ((last (cons arg nil))) | |
(setf (cdr ,gtail) last | |
,gtail last) | |
arg))))) | |
(let ((gtails (mapcar #'(lambda (s) (gensym "WITH-COLLECTS-TAIL")) syms))) | |
`(let ,syms | |
(labels | |
(,@(mapcar #'collector-expander syms gtails)) | |
,@body))))) | |
;; example | |
(with-collect (a b) | |
(a 10) | |
(b 20) | |
(a 30) | |
(list a b)) | |
=> ((10 30) 20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment