5 ;;; Anaphoric extensions
7 ;;; (c) 2005 Straylight/Edgeware
10 ;;;----- Licensing notice ---------------------------------------------------
12 ;;; This program is free software; you can redistribute it and/or modify
13 ;;; it under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 2 of the License, or
15 ;;; (at your option) any later version.
17 ;;; This program is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with this program; if not, write to the Free Software Foundation,
24 ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 (defpackage #:mdw.anaphora
29 #:aif #:aif2 #:awhen #:awhen2
30 #:aand #:awhile #:asetf #:acond))
31 (in-package #:mdw.anaphora)
33 (defmacro aif (cond then &optional else)
34 "Bind `it' to result of COND when evaluating THEN or ELSE."
36 (if it ,then ,@(and else (list else)))))
38 (defmacro aif2 (cond then &optional else)
39 "Bind `it' to first value of COND; switch on second."
41 `(multiple-value-bind (it ,tmp) ,cond
42 (declare (ignorable it))
43 (if ,tmp ,then ,@(and else (list else))))))
45 (defmacro awhen (cond &body body)
46 "Bind `it' to result of COND when evaluating BODY."
50 (defmacro awhen2 (cond &body body)
51 "Bind `it' to first value of COND; switch on second."
53 `(multiple-value-bind (it ,tmp) ,cond
54 (declare (ignorable it))
57 (defmacro aand (&rest things)
58 "Like `and', with `it' bound to previous value."
59 (labels ((foo (things)
61 `(let ((it ,(car things)))
62 (if it ,(foo (cdr things))))
68 (defmacro awhile (cond &body body)
69 "Like `while', with `it' bound to value of COND in BODY."
75 (defmacro asetf (&rest pairs &environment env)
76 "Set PLACE to value of FORM; in FORM, `it' is bound to current value of
80 (let ((place (car pairs))
83 (cons (multiple-value-bind
84 (valtmps valforms newtmps setform getform)
85 (get-setf-expansion place env)
86 `(let* ,(mapcar #'list valtmps valforms)
88 (,(car newtmps) ,form))
91 (cons 'progn (foo pairs))))
93 (defmacro acond (&rest clauses)
94 "Like `cond', but in each clause the consequent has `it' bound to the value
96 (labels ((foo (clauses)
99 (clause (car clauses)))
100 `(let ((,tmp ,(car clause)))
103 (declare (ignorable it))
105 ,(foo (cdr clauses))))))))
108 ;;;----- That's all, folks --------------------------------------------------