Extract the body of define-constant macro into a function to avoid some warnings.
[alexandria.git] / binding.lisp
blob6a4cc0575512c15a31b975b17d3782f347cd35d6
1 (in-package :alexandria)
3 (defmacro if-let (bindings 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)
15 ...
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
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))))
33 (defmacro if-let* (bindings then-form &optional else-form)
34 "Creates new variable bindings, and conditionally executes either THEN-FORM
35 or ELSE-FORM. ELSE-FORM defaults to NIL.
37 BINDINGS must be either single binding of the form:
39 (variable initial-form)
41 or a list of bindings of the form:
43 ((variable-1 initial-form-1)
44 (variable-2 initial-form-2)
45 ...
46 (variable-n initial-form-n))
48 Each initial-form is executed in turn, and the variable bound to the
49 corresponding value. Initial-form expressions can refer to variables
50 previously bound by the IF-LET*.
52 If all variables are true after the bindings are complete, the THEN-FORM is
53 executed with the bindings in effect, otherwise the ELSE-FORM is executed with
54 the bindings in effect."
55 (let* ((binding-list (if (and (consp bindings) (symbolp (car bindings)))
56 (list bindings)
57 bindings))
58 (variables (mapcar #'car binding-list)))
59 `(let* ,binding-list
60 (if (and ,@variables)
61 ,then-form
62 ,else-form))))
64 (defmacro when-let (bindings &body forms)
65 "Creates new variable bindings, and conditionally executes FORMS.
67 BINDINGS must be either single binding of the form:
69 (variable initial-form)
71 or a list of bindings of the form:
73 ((variable-1 initial-form-1)
74 (variable-2 initial-form-2)
75 ...
76 (variable-n initial-form-n))
78 All initial-forms are executed sequentially in the specified order. Then all
79 the variables are bound to the corresponding values.
81 If all variables were bound to true values, then FORMS are executed as an
82 implicit PROGN."
83 (let* ((binding-list (if (and (consp bindings) (symbolp (car bindings)))
84 (list bindings)
85 bindings))
86 (variables (mapcar #'car binding-list)))
87 `(let ,binding-list
88 (when (and ,@variables)
89 ,@forms))))
91 (defmacro when-let* (bindings &body forms)
92 "Creates new variable bindings, and conditionally executes FORMS.
94 BINDINGS must be either single binding of the form:
96 (variable initial-form)
98 or a list of bindings of the form:
100 ((variable-1 initial-form-1)
101 (variable-2 initial-form-2)
103 (variable-n initial-form-n))
105 Each initial-form is executed in turn, and the variable bound to the
106 corresponding value. Initial-form expressions can refer to variables
107 previously bound by the IF-LET*.
109 If all variables are true after the bindings are complete, then FORMS are
110 executed as an implicit PROGN."
111 (let* ((binding-list (if (and (consp bindings) (symbolp (car bindings)))
112 (list bindings)
113 bindings))
114 (variables (mapcar #'car binding-list)))
115 `(let* ,binding-list
116 (when (and ,@variables)
117 ,@forms))))