【发布时间】:2014-04-11 10:16:34
【问题描述】:
Leonardo Borges has 将 a fantastic presentation 放在 Clojure 中的 Monads 上。在其中他描述了 Clojure 中的 reader monad using the following code:
;; Reader Monad
(def reader-m
{:return (fn [a]
(fn [_] a))
:bind (fn [m k]
(fn [r]
((k (m r)) r)))})
(defn ask [] identity)
(defn asks [f]
(fn [env]
(f env)))
(defn connect-to-db []
(do-m reader-m
[db-uri (asks :db-uri)]
(prn (format "Connected to db at %s" db-uri))))
(defn connect-to-api []
(do-m reader-m
[api-key (asks :api-key)
env (ask)]
(prn (format "Connected to api with key %s" api-key))))
(defn run-app []
(do-m reader-m
[_ (connect-to-db)
_ (connect-to-api)]
(prn "Done.")))
((run-app) {:db-uri "user:passwd@host/dbname" :api-key "AF167"})
;; "Connected to db at user:passwd@host/dbname"
;; "Connected to api with key AF167"
;; "Done."
这样做的好处是您以纯粹的函数方式从环境中读取值。
但是这种方法看起来非常类似于 Clojure 中的偏函数。考虑以下代码:
user=> (def hundred-times (partial * 100))
#'user/hundred-times
user=> (hundred-times 5)
500
user=> (hundred-times 4 5 6)
12000
我的问题是:Reader monad 和 Clojure 中的偏函数有什么区别?
【问题讨论】:
标签: clojure monads partial-application reader-monad