| 1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
| 2 |
<HTML> |
| 3 |
<HEAD> |
| 4 |
<TITLE>Function COMPOSE</TITLE> |
| 5 |
<LINK REL="stylesheet" HREF="style.css" type="text/css"> |
| 6 |
</HEAD> |
| 7 |
<BODY> |
| 8 |
|
| 9 |
<p><p><i>Function</i> <b>COMPOSE</b></p> |
| 10 |
|
| 11 |
<p><p><b>Syntax:</b></p> |
| 12 |
|
| 13 |
<p><p><b>compose</b> <i>function* <tt>=></tt> composite-function</i></p> |
| 14 |
|
| 15 |
<p><p><b>Arguments and Values:</b></p> |
| 16 |
|
| 17 |
<p><p><i>function</i>---a <i><a href="http://www.lispworks.com/documentation/HyperSpec/Body/glo_f.html#function_designator">function designator</a></i>.</p> |
| 18 |
|
| 19 |
<p><i>composite-function</i>---a <i>function</i>. |
| 20 |
|
| 21 |
<p><p><b>Description:</b></p> |
| 22 |
|
| 23 |
<p>Composes its arguments into a single composite function. All its |
| 24 |
arguments are assumed to designate functions which take one argument |
| 25 |
and return one argument. |
| 26 |
|
| 27 |
<p><tt>(funcall (compose f g) 42)</tt> is equivalent to <tt>(f (g |
| 28 |
42))</tt>. Composition is right-associative. |
| 29 |
|
| 30 |
<p><b>Examples:</b> |
| 31 |
|
| 32 |
<pre> |
| 33 |
;; Just to illustrate order of operations |
| 34 |
(defun 2* (x) (* 2 x)) |
| 35 |
|
| 36 |
|
| 37 |
(funcall (compose #'1+ #'1+) 1) => 3 |
| 38 |
(funcall (compose '1+ '2*) 5) => 11 |
| 39 |
(funcall (compose #'1+ '2* '1+) 6) => 15 |
| 40 |
</pre> |
| 41 |
|
| 42 |
<p><b>Notes:</b> |
| 43 |
<p>If you're dealing with multiple arguments and return values, the |
| 44 |
same concept can be used. Here is some code that could be useful: |
| 45 |
|
| 46 |
<pre> |
| 47 |
(defun mv-compose2 (f1 f2) |
| 48 |
(lambda (&rest args) |
| 49 |
(multiple-value-call f1 (apply f2 args)))) |
| 50 |
|
| 51 |
(defun mv-compose (&rest functions) |
| 52 |
(if functions |
| 53 |
(reduce #'mv-compose2 functions) |
| 54 |
#'values)) |
| 55 |
</pre> |
| 56 |
|
| 57 |
<p class="footer"><hr><a href="index.html">Manual Index</a></p> |
| 58 |
|
| 59 |
</body></html> |