diff options
Diffstat (limited to 'hofs.lisp')
-rw-r--r-- | hofs.lisp | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/hofs.lisp b/hofs.lisp new file mode 100644 index 0000000..7eb1d51 --- /dev/null +++ b/hofs.lisp @@ -0,0 +1,23 @@ +;;; hofs.lisp -- higher-order functions + +(in-package :lambda-riffs) + +(defun labmda-if (pred then &optional (else (constantly nil))) + "Returns a function that applies PRED and conditionally executes +THEN when PRED returned non-nil or otherwise executes ELSE. Each of +PRED, THEN, and ELSE are assumed to accept the same arguments." + (lambda (&rest args) + (if (apply pred args) + (apply then args) + (apply else args)))) + +(defun lambda-cond (&rest pairs) + "PAIRS is a list: PRED1 F1 PRED2 F2 ... + Returns a function that applies FN to the argumetns for the first N + such that PREDN returns non-nil. Each of the PREDN and FN are + assumed to accept the same arguments." + (lambda (&rest args) + (loop for (pred fn . more) on pairs by #'cddr + when (apply pred args) + return (apply fn args)))) + |