[alexandria-devel] SOMETIMES

Tobias C. Rittweiler tcr at freebits.de
Thu Jan 17 17:03:53 UTC 2008


I'd also like to propose the following addition:

  (defun sometimes (test then &optional (else nil else-p))
    "Returns a function that applies TEST on its arguments and returns
    either THEN or ELSE depending on the test result.

    If ELSE is not provided, the arguments passed to the returned function
    are returned as multiple values.

    Examples:

    (mapcar (sometimes #'plusp 1 -1) '(1 -1 2 -2 3 -3))

      ==> (1 -1 1 -1 1 -1)

    (mapcar (sometimes #'minusp 0) '(1 -1 2 -2 3 -3))

      ==> (1 0 2 0 3 0)
    "
    (flet ((%sometimes (&rest args)
             (if (apply test args)
                 then
                 (if else-p else (values-list args)))))
      #'%sometimes))

It's in the same spirit as CONSTANTLY, which is also the reason for its
name. (SOMETIMES #'PLUSP 1 -1) can be read as

  sometimes 1, sometimes -1, depending on PLUSP.

The internal FLET should probably be replaced by a call to
ALEXANDRIA:NAMED-LET.

  -T.




More information about the alexandria-devel mailing list