Skip to content

Instantly share code, notes, and snippets.

@llibra
Created August 22, 2011 16:44
Show Gist options
  • Save llibra/1162860 to your computer and use it in GitHub Desktop.
Save llibra/1162860 to your computer and use it in GitHub Desktop.
do and multiple values
(defmacro do-mvb (var-bindings end-and-result &body body)
(let* ((bindings nil)
(first-time-p (gensym))
(var-bindings-
(reduce (lambda (result binding)
(if (consp binding)
(destructuring-bind (value . form) binding
(cond ((consp value)
(push (cons value form) bindings)
(append result value))
(t (append result (list binding)))))
binding))
var-bindings
:initial-value (list `(,first-time-p t nil)))))
`(do ,var-bindings-
,end-and-result
,@(mapcar (lambda (binding)
`(if ,first-time-p
(multiple-value-setq ,(car binding) ,(cadr binding))
,(if (caddr binding)
`(multiple-value-setq ,(car binding)
,(caddr binding)))))
bindings)
,@body)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment