Last active
December 17, 2015 02:19
-
-
Save knjname/5534989 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; カリー化 | |
| (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) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://d.hatena.ne.jp/Isuzu_T/20130507/1367935014 を見て触発された