more error-checking in RANDOM-ELT
[alexandria.git] / functions.lisp
blob15032be27e144722b4498849268224deba0e94ea
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 "Multiple-place modify macro for ENSURE-FUNCTION: ensures that each of
19 PLACES contains a function."
20 `(progn ,@(mapcar (lambda (x) `(ensure-functionf/1 ,x)) places)))
22 (defun disjoin (predicate &rest more-predicates)
23 "Returns a function that applies each of PREDICATE and MORE-PREDICATE
24 functions in turn to its arguments, returning the primary value of the first
25 predicate that returns true, without calling the remaining predicates.
26 If none of the predicates returns true, NIL is returned."
27 (declare (optimize (speed 3) (safety 1) (debug 1)))
28 (let ((predicate (ensure-function predicate))
29 (more-predicates (mapcar #'ensure-function more-predicates)))
30 (lambda (&rest arguments)
31 (or (apply predicate arguments)
32 (some (lambda (p)
33 (declare (type function p))
34 (apply p arguments))
35 more-predicates)))))
37 (defun conjoin (predicate &rest more-predicates)
38 "Returns a function that applies each of PREDICATE and MORE-PREDICATE
39 functions in turn to its arguments, returning NIL if any of the predicates
40 returns false, without calling the remaining predicates. If none of the
41 predicates returns false, returns the primary value of the last predicate."
42 (if (null more-predicates)
43 predicate
44 (lambda (&rest arguments)
45 (and (apply predicate arguments)
46 ;; Cannot simply use CL:EVERY because we want to return the
47 ;; non-NIL value of the last predicate if all succeed.
48 (do ((tail (cdr more-predicates) (cdr tail))
49 (head (car more-predicates) (car tail)))
50 ((not tail)
51 (apply head arguments))
52 (unless (apply head arguments)
53 (return nil)))))))
56 (defun compose (function &rest more-functions)
57 "Returns a function composed of FUNCTION and MORE-FUNCTIONS that applies its
58 arguments to to each in turn, starting from the rightmost of MORE-FUNCTIONS,
59 and then calling the next one with the primary value of the last."
60 (declare (optimize (speed 3) (safety 1) (debug 1)))
61 (reduce (lambda (f g)
62 (let ((f (ensure-function f))
63 (g (ensure-function g)))
64 (lambda (&rest arguments)
65 (declare (dynamic-extent arguments))
66 (funcall f (apply g arguments)))))
67 more-functions
68 :initial-value function))
70 (define-compiler-macro compose (function &rest more-functions)
71 (labels ((compose-1 (funs)
72 (if (cdr funs)
73 `(funcall ,(car funs) ,(compose-1 (cdr funs)))
74 `(apply ,(car funs) arguments))))
75 (let* ((args (cons function more-functions))
76 (funs (make-gensym-list (length args) "COMPOSE")))
77 `(let ,(loop for f in funs for arg in args
78 collect `(,f (ensure-function ,arg)))
79 (declare (optimize (speed 3) (safety 1) (debug 1)))
80 (lambda (&rest arguments)
81 (declare (dynamic-extent arguments))
82 ,(compose-1 funs))))))
84 (defun multiple-value-compose (function &rest more-functions)
85 "Returns a function composed of FUNCTION and MORE-FUNCTIONS that applies
86 its arguments to each in turn, starting from the rightmost of
87 MORE-FUNCTIONS, and then calling the next one with all the return values of
88 the last."
89 (declare (optimize (speed 3) (safety 1) (debug 1)))
90 (reduce (lambda (f g)
91 (let ((f (ensure-function f))
92 (g (ensure-function g)))
93 (lambda (&rest arguments)
94 (declare (dynamic-extent arguments))
95 (multiple-value-call f (apply g arguments)))))
96 more-functions
97 :initial-value function))
99 (define-compiler-macro multiple-value-compose (function &rest more-functions)
100 (labels ((compose-1 (funs)
101 (if (cdr funs)
102 `(multiple-value-call ,(car funs) ,(compose-1 (cdr funs)))
103 `(apply ,(car funs) arguments))))
104 (let* ((args (cons function more-functions))
105 (funs (make-gensym-list (length args) "MV-COMPOSE")))
106 `(let ,(mapcar #'list funs args)
107 (declare (optimize (speed 3) (safety 1) (debug 1)))
108 (lambda (&rest arguments)
109 (declare (dynamic-extent arguments))
110 ,(compose-1 funs))))))
112 (defun curry (function &rest arguments)
113 "Returns a function that applies ARGUMENTS and the arguments
114 it is called with to FUNCTION."
115 (declare (optimize (speed 3) (safety 1) (debug 1)))
116 (let ((fn (ensure-function function)))
117 (lambda (&rest more)
118 (declare (dynamic-extent more))
119 ;; Using M-V-C we don't need to append the arguments.
120 (multiple-value-call fn (values-list arguments) (values-list more)))))
122 (define-compiler-macro curry (function &rest arguments)
123 (let ((curries (make-gensym-list (length arguments) "CURRY")))
124 `(let ,(mapcar #'list curries arguments)
125 (declare (optimize (speed 3) (safety 1) (debug 1)))
126 (lambda (&rest more)
127 (apply ,function ,@curries more)))))
129 (defun rcurry (function &rest arguments)
130 "Returns a function that applies the arguments it is called
131 with and ARGUMENTS to FUNCTION."
132 (declare (optimize (speed 3) (safety 1) (debug 1)))
133 (let ((fn (ensure-function function)))
134 (lambda (&rest more)
135 (declare (dynamic-extent more))
136 (multiple-value-call fn (values-list more) (values-list arguments)))))
138 (defmacro named-lambda (name lambda-list &body body)
139 "Expands into a lambda-expression within whose BODY NAME denotes the
140 corresponding function."
141 `(labels ((,name ,lambda-list ,@body))
142 #',name))