Created
February 2, 2012 04:10
-
-
Save cvmat/1721391 to your computer and use it in GitHub Desktop.
and-let* for Emacs LISP
This file contains 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 and-let* (bindings &rest body) | |
(let ((head (car bindings)) | |
(rest (cdr bindings))) | |
(cond | |
((null head) | |
`(progn | |
,@body)) | |
((and (null rest) (null body)) | |
(cond | |
((symbolp head) | |
head) | |
((symbolp (car head)) | |
(let ((exp (cadr head))) | |
exp)) | |
((listp (car head)) | |
(let ((exp (car head))) | |
exp)))) | |
((symbolp head) | |
`(if ,head | |
(and-let* ,rest ,@body) | |
nil)) | |
((symbolp (car head)) | |
(let ((var (car head)) | |
(exp (cadr head)) | |
(tmp-var (gensym))) | |
`(let ((,tmp-var ,exp)) | |
(if ,tmp-var | |
(let ((,var ,tmp-var)) | |
,(if (and (null rest) (null body)) | |
var | |
`(and-let* ,rest ,@body))) | |
nil)))) | |
((listp (car head)) | |
(let ((exp (car head))) | |
`(if ,exp | |
(and-let* ,rest ,@body) | |
nil))) | |
(t | |
nil)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment