Paste number 82993: untitled

Paste number 82993: untitled
Pasted by: ???
When:2 years, 7 months ago
Share:Tweet this! | http://paste.lisp.org/+1S1D
Channel:None
Paste contents:
Raw Source | XML | Display As
(defun compose (&rest fns) 
  (destructuring-bind (fn1 . rest) (reverse fns)
    #'(lambda (&rest args)
	(reduce #'(lambda (v f) (funcall f v))
		rest
		:initial-value (apply fn1 args)))))

(defun compose (&rest fns) 
  "Compose takes a number of functions and returns their composition"
  (destructuring-bind (fn1 . rest) (reverse fns) ; destructuring-bind is a generalized form of let, 
					;it takes a lambda list tree and a list/tree and binds the variables specified in it
					;the same could have been done using let and car/cdr to access the list
    #'(lambda (&rest args) ; the composed function is returned, it takes any number of arguments
	(reduce #'(lambda (v f) (funcall f v)); reducing is done by taking a value and a function and applying the function to the value
		rest ; rest represents the remaining functions to be composed (in reverse order)
		:initial-value (apply fn1 args))))) ; the first value which is passed 
					;to the reduce function is the result of applying
					;given arguments to the first function
					;All but the first function take a single argument

This paste has no annotations.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.