Skip to content

Instantly share code, notes, and snippets.

@denlab
Created August 25, 2012 17:59
Show Gist options
  • Save denlab/3468564 to your computer and use it in GitHub Desktop.
Save denlab/3468564 to your computer and use it in GitHub Desktop.
;; you are starting here
(fn [x & xs]
(fn [& args]
((fn step [[f & fs] a]
(if fs
(f (step fs a))
(apply f a)))
(cons x xs) args)))
;; name the fn
(defn mycomp
[x & xs] (fn [& args]
((fn step [[f & fs] a]
(if fs
(f (step fs a))
(apply f a)))
(cons x xs) args)))
;; extract the step function
(defn step "Equivalent to (apply (comp f & fs) args)"
[[f & fs] args] (if fs
(f (step fs args))
(apply f args)))
(defn mycomp "Equivalent to (partial step fns)"
[& fns] (fn [& args]
(step fns args)))
;; rename a few things for clarity
(defn comp-then-apply "Equivalent to (apply (comp f & fs) args)"
[[f & fs] args] (if fs
(f (comp-then-apply fs args))
(apply f args)))
(defn mycomp "Equivalent to (partial comp-then-apply fns)"
[& fns] (fn [& args]
(comp-then-apply fns args)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment