Skip to content

Instantly share code, notes, and snippets.

@fogus
Created August 18, 2010 19:37
Show Gist options
  • Save fogus/535901 to your computer and use it in GitHub Desktop.
Save fogus/535901 to your computer and use it in GitHub Desktop.
(defn comp+
([] identity)
([f] f)
([f g]
(fn
([] (f (g)))
([x] (f (g x)))
([x y] (f (g x y)))
([x y z] (f (g x y z)))
([x y z & args] (f (apply g x y z args)))))
([f g h]
(fn
([] (f (g (h))))
([x] (f (g (h x))))
([x y] (f (g (h x y))))
([x y z] (f (g (h x y z))))
([x y z & args] (f (g (apply h x y z args))))))
([f1 f2 f3 & fs]
(let [fs (reverse (list* f1 f2 f3 fs))]
(fn [& args]
(loop [ret (apply (first fs) args) fs (next fs)]
(if fs
(recur ((first fs) ret) (next fs))
ret))))))
;; allowing
(for [f (map #(apply comp+ %)
[[keyword name]
[]
[name clojure.string/upper-case]])
e '[foo bar]]
(f e))
;=> (:foo :bar foo bar "FOO" "BAR")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment