Skip to content

Instantly share code, notes, and snippets.

@munro

munro/curry.cljc Secret

Last active March 19, 2016 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save munro/df5191d998befdb8acdc to your computer and use it in GitHub Desktop.
Save munro/df5191d998befdb8acdc to your computer and use it in GitHub Desktop.
(defn curry [init depth]
(let [mcurry (fn [xs] (fn [x] (conj xs x)))
mbind (fn [f1 f2] (fn [xs] (fn [x] (f2 ((f1 xs) x)))))
mcurryable (reduce (fn [a _] (mbind mcurry a))
mcurry
(into [] (replicate (- depth 1) nil)))]
(if (= depth 0) init (mcurryable init))))
(t/deftest curry-test
(t/is (= ["meow-0"] (curry ["meow-0"] 0)))
(t/is (= ["meow-1" 1] ((curry ["meow-1"] 1) 1)))
(t/is (= ["meow-2" 1 2] (((curry ["meow-2"] 2) 1) 2)))
(t/is (= ["meow-4" 1 2 3 4] (((((curry ["meow-4"] 4) 1) 2) 3) 4))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment