Skip to content

Instantly share code, notes, and snippets.

@knjname
Last active December 17, 2015 02:19
Show Gist options
  • Select an option

  • Save knjname/5534989 to your computer and use it in GitHub Desktop.

Select an option

Save knjname/5534989 to your computer and use it in GitHub Desktop.
;; カリー化
(defn currize [[x & xs] body]
`(fn [~x]
~(if xs
(currize xs body)
`(do ~@body))))
(defmacro currying [params & body]
(currize params body))
;; これは
(currying [a b c d]
(+ (* a b) (/ c d)))
;; 下記に展開される
(clojure.core/fn [a]
(clojure.core/fn [b]
(clojure.core/fn [c]
(clojure.core/fn [d] (do (+ (* a b) (/ c d)))))))
;; 使ってみるけど、カッコがめんどい
((((currying [a b c] (+ a b c)) 10) 20) 30)
;; めんどいので、マクロつくる
(defn reverse-arrow [args body]
(if (empty? args)
body
(recur (rest args) `(~body ~(first args)))))
(defmacro <- [subject & args]
(reverse-arrow args subject))
;; こう書けるよ
(<- (currying [a b c] (+ a b c))
10 20 30)
;; こうなる
((((currying [a b c] (+ a b c)) 10) 20) 30)
;; 最終的にはこれ
((((fn* ([a] (fn* ([b] (fn* ([c] (do (+ a b c)))))))) 10) 20) 30)
@knjname
Copy link
Copy Markdown
Author

knjname commented May 7, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment