Add a compose macro that composes functions with arbitrary arguments

This commit is contained in:
Ivan Malison 2015-11-25 12:50:08 -05:00
parent b04bc8db5b
commit 99c20e04d6

View File

@ -204,6 +204,22 @@
;; functions ;; functions
;; ============================================================================= ;; =============================================================================
(defmacro imalison:compose (name &rest funcs)
"Build a new function with NAME that is the composition of FUNCS."
`(defun ,name (&rest args)
(imalison:compose-helper ,funcs)))
(defun imalison:make-list (thing)
(if (listp thing)
thing
(list thing)))
(defmacro imalison:compose-helper (funcs)
"Builds funcalls of FUNCS applied to the arg."
(if (equal (length funcs) 0)
(quote args)
`(apply ,(car funcs) (imalison:make-list (imalison:compose-helper ,(cdr funcs))))))
(defun random-choice (choices) (defun random-choice (choices)
(nth (random (length choices)) choices)) (nth (random (length choices)) choices))