| 1 |
(in-package :alexandria) |
| 2 |
|
| 3 |
(defmacro if-let (bindings &body (then-form &optional else-form)) |
| 4 |
"Creates new variable bindings, and conditionally executes either |
| 5 |
THEN-FORM or ELSE-FORM. ELSE-FORM defaults to NIL. |
| 6 |
|
| 7 |
BINDINGS must be either single binding of the form: |
| 8 |
|
| 9 |
(variable initial-form) |
| 10 |
|
| 11 |
or a list of bindings of the form: |
| 12 |
|
| 13 |
((variable-1 initial-form-1) |
| 14 |
(variable-2 initial-form-2) |
| 15 |
... |
| 16 |
(variable-n initial-form-n)) |
| 17 |
|
| 18 |
All initial-forms are executed sequentially in the specified order. Then all |
| 19 |
the variables are bound to the corresponding values. |
| 20 |
|
| 21 |
If all variables were bound to true values, the THEN-FORM is executed with the |
| 22 |
bindings in effect, otherwise the ELSE-FORM is executed with the bindings in |
| 23 |
effect." |
| 24 |
(let* ((binding-list (if (and (consp bindings) (symbolp (car bindings))) |
| 25 |
(list bindings) |
| 26 |
bindings)) |
| 27 |
(variables (mapcar #'car binding-list))) |
| 28 |
`(let ,binding-list |
| 29 |
(if (and ,@variables) |
| 30 |
,then-form |
| 31 |
,else-form)))) |
| 32 |
|
| 33 |
(defmacro when-let (bindings &body forms) |
| 34 |
"Creates new variable bindings, and conditionally executes FORMS. |
| 35 |
|
| 36 |
BINDINGS must be either single binding of the form: |
| 37 |
|
| 38 |
(variable initial-form) |
| 39 |
|
| 40 |
or a list of bindings of the form: |
| 41 |
|
| 42 |
((variable-1 initial-form-1) |
| 43 |
(variable-2 initial-form-2) |
| 44 |
... |
| 45 |
(variable-n initial-form-n)) |
| 46 |
|
| 47 |
All initial-forms are executed sequentially in the specified order. Then all |
| 48 |
the variables are bound to the corresponding values. |
| 49 |
|
| 50 |
If all variables were bound to true values, then FORMS are executed as an |
| 51 |
implicit PROGN." |
| 52 |
(let* ((binding-list (if (and (consp bindings) (symbolp (car bindings))) |
| 53 |
(list bindings) |
| 54 |
bindings)) |
| 55 |
(variables (mapcar #'car binding-list))) |
| 56 |
`(let ,binding-list |
| 57 |
(when (and ,@variables) |
| 58 |
,@forms)))) |
| 59 |
|
| 60 |
(defmacro when-let* (bindings &body body) |
| 61 |
"Creates new variable bindings, and conditionally executes BODY. |
| 62 |
|
| 63 |
BINDINGS must be either single binding of the form: |
| 64 |
|
| 65 |
(variable initial-form) |
| 66 |
|
| 67 |
or a list of bindings of the form: |
| 68 |
|
| 69 |
((variable-1 initial-form-1) |
| 70 |
(variable-2 initial-form-2) |
| 71 |
... |
| 72 |
(variable-n initial-form-n)) |
| 73 |
|
| 74 |
Each INITIAL-FORM is executed in turn, and the variable bound to the |
| 75 |
corresponding value. INITIAL-FORM expressions can refer to variables |
| 76 |
previously bound by the WHEN-LET*. |
| 77 |
|
| 78 |
Execution of WHEN-LET* stops immediately if any INITIAL-FORM evaluates to NIL. |
| 79 |
If all INITIAL-FORMs evaluate to true, then BODY is executed as an implicit |
| 80 |
PROGN." |
| 81 |
(let ((binding-list (if (and (consp bindings) (symbolp (car bindings))) |
| 82 |
(list bindings) |
| 83 |
bindings))) |
| 84 |
(labels ((bind (bindings body) |
| 85 |
(if bindings |
| 86 |
`(let (,(car bindings)) |
| 87 |
(when ,(caar bindings) |
| 88 |
,(bind (cdr bindings) body))) |
| 89 |
`(progn ,@body)))) |
| 90 |
(bind binding-list body)))) |