1 (in-package :alexandria
)
3 (defun extract-function-name (spec)
4 "Useful for macros that want to mimic the functional interface for functions
7 (member (first spec
) '(quote function
)))
11 (defun generate-switch-body (whole object clauses test key
&optional default
)
13 (setf test
(extract-function-name test
))
14 (setf key
(extract-function-name key
))
15 (when (and (consp default
)
16 (member (first default
) '(error cerror
)))
17 (setf default
`(,@default
"No keys match in SWITCH. Testing against ~S with ~S."
19 `(let ((,value
(,key
,object
)))
20 (cond ,@(mapcar (lambda (clause)
21 (if (member (first clause
) '(t otherwise
))
24 (error "Multiple default clauses or illegal use of a default clause in ~S."
26 (setf default
`(progn ,@(rest clause
)))
28 (destructuring-bind (key-form &body forms
) clause
29 `((,test
,value
,key-form
)
34 (defmacro switch
(&whole whole
(object &key
(test 'eql
) (key 'identity
))
36 "Evaluates first matching clause, returning its values, or evaluates and
37 returns the values of DEFAULT if no keys match."
38 (generate-switch-body whole object clauses test key
))
40 (defmacro eswitch
(&whole whole
(object &key
(test 'eql
) (key 'identity
))
42 "Like SWITCH, but signals an error if no key matches."
43 (generate-switch-body whole object clauses test key
'(error)))
45 (defmacro cswitch
(&whole whole
(object &key
(test 'eql
) (key 'identity
))
47 "Like SWITCH, but signals a continuable error if no key matches."
48 (generate-switch-body whole object clauses test key
'(cerror "Return NIL from CSWITCH.")))
50 (defmacro whichever
(&rest possibilities
&environment env
)
51 "Evaluates exactly one of POSSIBILITIES, chosen at random."
52 (setf possibilities
(mapcar (lambda (p) (macroexpand p env
)) possibilities
))
53 (if (every (lambda (p) (constantp p
)) possibilities
)
54 `(svref (load-time-value (vector ,@possibilities
)) (random ,(length possibilities
)))
55 (with-gensyms (function)
56 `(let ((,function
(lambda () ,(pop possibilities
))))
57 (declare (function ,function
))
59 (mapcar (lambda (possibility)
60 `(when (zerop (random ,(incf p
)))
61 (setf ,function
(lambda () ,possibility
))))
63 (funcall ,function
)))))
65 (defmacro xor
(&rest datums
)
66 "Evaluates its argument one at a time, from left to right. If more then one
67 argument evaluates to a true value no further DATUMS are evaluated, and NIL is
68 returned as both primary and secondary value. If exactly one argument
69 evaluates to true, its value is returned as the primary value after all the
70 arguments have been evaluated, and T is returned as the secondary value. If no
71 arguments evaluate to true NIL is retuned as primary, and T as secondary
73 (with-gensyms (xor tmp true
)
76 ,@(mapcar (lambda (datum)
77 `(if (setf ,tmp
,datum
)
79 (return-from ,xor
(values nil nil
))
82 (return-from ,xor
(values ,true t
))))))
84 (defmacro nth-value-or
(nth-value &body forms
)
85 "Evaluates FORM arguments one at a time, until the NTH-VALUE returned by one
86 of the forms is non-NIL. It then returns all the values returned by evaluating
87 that form. If none of the forms return a non-nil nth value, this form returns
89 (once-only (nth-value)
90 (with-gensyms (values)
91 `(let ((,values
(multiple-value-list ,(first forms
))))
92 (if (nth ,nth-value
,values
)
95 `(nth-value-or ,nth-value
,@(rest forms
))
98 (defmacro multiple-value-prog2
(first-form second-form
&body body
)
99 "Like CL:MULTIPLE-VALUE-PROG1, except it saves the values of the
101 `(progn ,first-form
(multiple-value-prog1 ,second-form
,@body
)))