Function COMPOSE

Syntax:

compose function* => composite-function

Arguments and Values:

function---a function designator.

composite-function---a function.

Description:

Composes its arguments into a single composite function. All its arguments are assumed to designate functions which take one argument and return one argument.

(funcall (compose f g) 42) is equivalent to (f (g 42)). Composition is right-associative.

Examples:

;; Just to illustrate order of operations
(defun 2* (x) (* 2 x))


(funcall (compose #'1+ #'1+) 1) => 3
(funcall (compose '1+ '2*) 5) => 11
(funcall (compose #'1+ '2* '1+) 6) => 15 

Notes:

If you're dealing with multiple arguments and return values, the same concept can be used. Here is some code that could be useful:

(defun mv-compose2 (f1 f2)
  (lambda (&rest args)
    (multiple-value-call f1 (apply f2 args))))
    
(defun mv-compose (&rest functions)
  (if functions
    (reduce #'mv-compose2 functions)
    #'values))

Manual Index