binding.lisp 2.97 KB
Newer Older
1 2
(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.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

BINDINGS must be either single binding of the form:

 (variable initial-form)

or a list of bindings of the form:

 ((variable-1 initial-form-1)
  (variable-2 initial-form-2)
  ...
  (variable-n initial-form-n))

All initial-forms are executed sequentially in the specified order. Then all
the variables are bound to the corresponding values.

If all variables were bound to true values, the THEN-FORM is executed with the
bindings in effect, otherwise the ELSE-FORM is executed with the bindings in
effect."
    (let* ((binding-list (if (and (consp bindings) (symbolp (car bindings)))
                             (list bindings)
                             bindings))
         (variables (mapcar #'car binding-list)))
    `(let ,binding-list
       (if (and ,@variables)
           ,then-form
           ,else-form))))

(defmacro when-let (bindings &body forms)
34
    "Creates new variable bindings, and conditionally executes FORMS.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

BINDINGS must be either single binding of the form:

 (variable initial-form)

or a list of bindings of the form:

 ((variable-1 initial-form-1)
  (variable-2 initial-form-2)
  ...
  (variable-n initial-form-n))

All initial-forms are executed sequentially in the specified order. Then all
the variables are bound to the corresponding values.

If all variables were bound to true values, then FORMS are executed as an
implicit PROGN."
  (let* ((binding-list (if (and (consp bindings) (symbolp (car bindings)))
                           (list bindings)
                           bindings))
         (variables (mapcar #'car binding-list)))
    `(let ,binding-list
       (when (and ,@variables)
         ,@forms))))

60 61
(defmacro when-let* (bindings &body body)
  "Creates new variable bindings, and conditionally executes BODY.
62 63 64 65 66 67 68 69 70 71 72 73

BINDINGS must be either single binding of the form:

 (variable initial-form)

or a list of bindings of the form:

 ((variable-1 initial-form-1)
  (variable-2 initial-form-2)
  ...
  (variable-n initial-form-n))

74 75
Each INITIAL-FORM is executed in turn, and the variable bound to the
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 81
PROGN."
  (let ((binding-list (if (and (consp bindings) (symbolp (car bindings)))
82 83
                          (list bindings)
                          bindings)))
84
    (labels ((bind (bindings body)
85
               (if bindings
86 87 88 89 90 91 92
                   `((let (,(car bindings))
                       (when ,(caar bindings)
                         ,@(bind (cdr bindings) body))))
                   body)))
      `(let (,(car binding-list))
         (when ,(caar binding-list)
           ,@(bind (cdr binding-list) body))))))
93