Project author Jean-Philippe Paradis
Project type Library
Programming language Common Lisp
Status Ready-to-use
License Public Domain
Library type Operator overlay macro
Project complexity Simple
Latest release Version 2.0
Date 2 sept 2012
Quicklisp
(Sept 2012 dist and later)
Status Available
Version Latest
Depends on Depended on by
Directly
Indirectly Nothing Nothing

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:

It's also possible to make your own kinds. Check definitions.lisp in particular for inspiration. In a nutshell, call (cartesian-product-switch:define operator (args) body), where body must return 3 values:

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".