faster WHICHEVER
[alexandria.git] / functions.lisp
blob8d2fd38f3a97fe9e1d791b2e8eb141223b47cf39
1 (in-package :alexandria)
3 (declaim (inline ensure-function)) ; to propagate return type.
5 (declaim (ftype (function (t) (values function &optional))
6 ensure-function))
7 (defun ensure-function (function-designator)
8 "Returns the function designated by FUNCTION-DESIGNATOR:
9 if FUNCTION-DESIGNATOR is a function, it is returned, otherwise
10 it must be a function name and its FDEFINITION is returned."
11 (if (functionp function-designator)
12 function-designator
13 (fdefinition function-designator)))
15 (define-modify-macro ensure-functionf/1 () ensure-function)
17 (defmacro ensure-functionf (&rest places)
18 `(progn ,@(mapcar (lambda (x) `(ensure-functionf/1 ,x)) places)))
20 (defun disjoin (predicate &rest more-predicates)
21 "Returns a function that applies each of PREDICATE and MORE-PREDICATE
22 functions in turn to its arguments, returning the primary value of the first
23 predicate that returns true, without calling the remaining predicates.
24 If none of the predicates returns true, NIL is returned."
25 (declare (optimize (speed 3) (safety 1) (debug 1)))
26 (let ((predicate (ensure-function predicate))
27 (more-predicates (mapcar #'ensure-function more-predicates)))
28 (lambda (&rest arguments)
29 (or (apply predicate arguments)
30 (some (lambda (p)
31 (declare (type function p))
32 (apply p arguments))
33 more-predicates)))))
35 (defun conjoin (predicate &rest more-predicates)
36 "Returns a function that applies each of PREDICATE and MORE-PREDICATE
37 functions in turn to its arguments, returning NIL if any of the predicates
38 returns false, without calling the remaining predicates. If none of the
39 predicates returns false, returns the primary value of the last predicate."
40 (if (null more-predicates)
41 predicate
42 (lambda (&rest arguments)
43 (and (apply predicate arguments)
44 ;; Cannot simply use CL:EVERY because we want to return the
45 ;; non-NIL value of the last predicate if all succeed.
46 (do ((tail (cdr more-predicates) (cdr tail))
47 (head (car more-predicates) (car tail)))
48 ((not tail)
49 (apply head arguments))
50 (unless (apply head arguments)
51 (return nil)))))))
54 (defun compose (function &rest more-functions)
55 "Returns a function composed of FUNCTION and MORE-FUNCTIONS that applies its
56 arguments to to each in turn, starting from the rightmost of MORE-FUNCTIONS,
57 and then calling the next one with the primary value of the last."
58 (declare (optimize (speed 3) (safety 1) (debug 1)))
59 (reduce (lambda (f g)
60 (let ((f (ensure-function f))
61 (g (ensure-function g)))
62 (lambda (&rest arguments)
63 (declare (dynamic-extent arguments))
64 (funcall f (apply g arguments)))))
65 more-functions
66 :initial-value function))
68 (define-compiler-macro compose (function &rest more-functions)
69 (labels ((compose-1 (funs)
70 (if (cdr funs)
71 `(funcall ,(car funs) ,(compose-1 (cdr funs)))
72 `(apply ,(car funs) arguments))))
73 (let* ((args (cons function more-functions))
74 (funs (make-gensym-list (length args) "COMPOSE")))
75 `(let ,(loop for f in funs for arg in args
76 collect `(,f (ensure-function ,arg)))
77 (declare (optimize (speed 3) (safety 1) (debug 1)))
78 (lambda (&rest arguments)
79 (declare (dynamic-extent arguments))
80 ,(compose-1 funs))))))
82 (defun multiple-value-compose (function &rest more-functions)
83 "Returns a function composed of FUNCTION and MORE-FUNCTIONS that applies
84 its arguments to each in turn, starting from the rightmost of
85 MORE-FUNCTIONS, and then calling the next one with all the return values of
86 the last."
87 (declare (optimize (speed 3) (safety 1) (debug 1)))
88 (reduce (lambda (f g)
89 (let ((f (ensure-function f))
90 (g (ensure-function g)))
91 (lambda (&rest arguments)
92 (declare (dynamic-extent arguments))
93 (multiple-value-call f (apply g arguments)))))
94 more-functions
95 :initial-value function))
97 (define-compiler-macro multiple-value-compose (function &rest more-functions)
98 (labels ((compose-1 (funs)
99 (if (cdr funs)
100 `(multiple-value-call ,(car funs) ,(compose-1 (cdr funs)))
101 `(apply ,(car funs) arguments))))
102 (let* ((args (cons function more-functions))
103 (funs (make-gensym-list (length args) "MV-COMPOSE")))
104 `(let ,(mapcar #'list funs args)
105 (declare (optimize (speed 3) (safety 1) (debug 1)))
106 (lambda (&rest arguments)
107 (declare (dynamic-extent arguments))
108 ,(compose-1 funs))))))
110 (defun curry (function &rest arguments)
111 "Returns a function that applies ARGUMENTS and the arguments
112 it is called with to FUNCTION."
113 (declare (optimize (speed 3) (safety 1) (debug 1)))
114 (let ((fn (ensure-function function)))
115 (lambda (&rest more)
116 (declare (dynamic-extent more))
117 ;; Using M-V-C we don't need to append the arguments.
118 (multiple-value-call fn (values-list arguments) (values-list more)))))
120 (define-compiler-macro curry (function &rest arguments)
121 (let ((curries (make-gensym-list (length arguments) "CURRY")))
122 `(let ,(mapcar #'list curries arguments)
123 (declare (optimize (speed 3) (safety 1) (debug 1)))
124 (lambda (&rest more)
125 (apply ,function ,@curries more)))))
127 (defun rcurry (function &rest arguments)
128 "Returns a function that applies the arguments it is called
129 with and ARGUMENTS to FUNCTION."
130 (declare (optimize (speed 3) (safety 1) (debug 1)))
131 (let ((fn (ensure-function function)))
132 (lambda (&rest more)
133 (declare (dynamic-extent more))
134 (multiple-value-call fn (values-list more) (values-list arguments)))))
136 (defmacro named-lambda (name lambda-list &body body)
137 "Expands into a lambda-expression within whose BODY NAME denotes the
138 corresponding function."
139 `(labels ((,name ,lambda-list ,@body))
140 #',name))