1 (in-package :alexandria
)
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.
7 BINDINGS must be either single binding of the form:
9 (variable initial-form)
11 or a list of bindings of the form:
13 ((variable-1 initial-form-1)
14 (variable-2 initial-form-2)
16 (variable-n initial-form-n))
18 All initial-forms are executed sequentially in the specified order. Then all
19 the variables are bound to the corresponding values.
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
24 (let* ((binding-list (if (and (consp bindings
) (symbolp (car bindings
)))
27 (variables (mapcar #'car binding-list
)))
33 (defmacro when-let
(bindings &body forms
)
34 "Creates new variable bindings, and conditionally executes FORMS.
36 BINDINGS must be either single binding of the form:
38 (variable initial-form)
40 or a list of bindings of the form:
42 ((variable-1 initial-form-1)
43 (variable-2 initial-form-2)
45 (variable-n initial-form-n))
47 All initial-forms are executed sequentially in the specified order. Then all
48 the variables are bound to the corresponding values.
50 If all variables were bound to true values, then FORMS are executed as an
52 (let* ((binding-list (if (and (consp bindings
) (symbolp (car bindings
)))
55 (variables (mapcar #'car binding-list
)))
57 (when (and ,@variables
)
60 (defmacro when-let
* (bindings &body forms
)
61 "Creates new variable bindings, and conditionally executes FORMS.
63 BINDINGS must be either single binding of the form:
65 (variable initial-form)
67 or a list of bindings of the form:
69 ((variable-1 initial-form-1)
70 (variable-2 initial-form-2)
72 (variable-n initial-form-n))
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*.
78 Execution of WHEN-LET* stops immediately if any initial-form evaluates to NIL.
79 If all initial-forms evaluate to true, then FORMS are executed as an implicit
81 (let ((binding-list (if (and (consp bindings
) (symbolp (car bindings
)))
84 (labels ((bind (bindings forms
)
86 `((let (,(car bindings
))
87 (when ,(caar bindings
)
88 ,@(bind (cdr bindings
) forms
))))
90 `(let (,(car binding-list
))
91 (when ,(caar binding-list
)
92 ,@(bind (cdr binding-list
) forms
))))))