Created
June 12, 2012 14:09
-
-
Save Wilfred/2917761 to your computer and use it in GitHub Desktop.
and-let* from Scheme implemented in Emacs Lisp
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
(eval-when-compile (require 'cl)); declare, first, rest | |
;;;; and-let*, based on Scheme's SRFI 2 | |
;; Contrived usage example: | |
;; (and-let* ((buffer-path (buffer-file-name)) | |
;; (file-name (file-name-directory))) | |
;; (message "You are visiting a file called %s in this buffer." | |
;; file-name)) | |
(defmacro and-let* (varlist &rest body) | |
"Equivalent to let*, but terminates early and returns nil if | |
any variable is bound to nil. BODY is only evaluated if all | |
variables were non-nil." | |
(declare (debug (form &rest form))) | |
(if (eq varlist '()) `(progn ,@body) | |
(let* ((first-binding (first varlist)) | |
(var-name (first first-binding))) | |
`(let (,first-binding) | |
(if ,var-name | |
(and-let* ,(rest varlist) ,@body)))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment