Function: COMPOSE

Documentation

Returns a function which applies the arguments in order. (funcall (compose #'list #'+) 1 2 3) ==> (6)

Source

(defun compose (f1 &rest functions)
  "Returns a function which applies the arguments in order.

 (funcall (compose #'list #'+) 1 2 3) ==> (6)"
  (case (length functions)
    (0 f1)
    (1 (lambda (&rest args)
         (funcall f1 (apply (car functions) args))))
    (2 (lambda (&rest args)
         (funcall f1
                  (funcall (first functions)
                           (apply (second functions) args)))))
    (3 (lambda (&rest args)
         (funcall f1
                  (funcall (first functions)
                           (funcall (second functions)
                                    (apply (third functions) args))))))
    (t
     (let ((funcs (nreverse (cons f1 functions))))
       (lambda (&rest args)
         (loop
            for f in funcs
            for r = (multiple-value-list (apply f args))
               then (multiple-value-list (apply f r))
            finally (return r)))))))
Source Context