From 99c20e04d65ec38101d7fe0396d4e00e808b6d51 Mon Sep 17 00:00:00 2001 From: Ivan Malison Date: Wed, 25 Nov 2015 12:50:08 -0500 Subject: [PATCH] Add a compose macro that composes functions with arbitrary arguments --- dotfiles/emacs.d/init.el | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/dotfiles/emacs.d/init.el b/dotfiles/emacs.d/init.el index c671a61b..2453897b 100644 --- a/dotfiles/emacs.d/init.el +++ b/dotfiles/emacs.d/init.el @@ -204,6 +204,22 @@ ;; 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) (nth (random (length choices)) choices))