Skip to content

Instantly share code, notes, and snippets.

@kurohuku
Created November 16, 2010 08:00
Show Gist options
  • Save kurohuku/701572 to your computer and use it in GitHub Desktop.
Save kurohuku/701572 to your computer and use it in GitHub Desktop.
(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