cartesian-product-switch
cartesian-product-switch
is a macro for choosing the appropriate form to execute according to the combined results of multiple tests.
This is a straightforward and efficient alternative to the convoluted ad-hoc conditionals one might otherwise resort to.
(let ((manner :flip) (direction :backward))
(cartesian-product-switch
((case
manner
:walk
:flip)
(case
direction
:forward
:in-place
:backward))
"Walking..."
(error "Walking in-place is too boring!"
)
"Awkwardly walking backwards..."
"Frontflip!"
"Jumping in-place."
"Backflip!"
(t
(error "Don't know how to ~A while going ~A."
manner direction))))
To use cartesian-product-switch
, simply (:import-from #:cartesian-product-switch #:cartesian-product-switch)
. Don't (:use)
!
Currently supported testclause kinds:
case
ccase
ecase
typecase
ctypecase
etypecase
if
cond
svref
It's also possible to make your own kinds. Check definitions.lisp in particular for inspiration. In a nutshell, call (
, where body must return 3 values:cartesian-product-switch:define
operator (args) body)
- selection-form
A form evaluating to a non-negative integer less than selection-count.
- selection-count
The number of possibilities this testclause introduces.
- defaultp
If true, then the selection-form can evaluate to
nil
to trigger the "default".