在Clojure 1.5.1的光照表下使用rxjava(http://netflix.github.io/RxJava/javadoc/),请考虑以下因素(使用副作用,变异(恐怖!)外部收集器从monad中提取数据)
(ns expt1.core (:import [rx]))
(def ^:private collector (atom []))
(defn- collect [item]
(reset! collector (conj @collector item)))
(def string-explode (partial map identity))
(reset! collector [])
(->
(Observable/toObservable ["one" "two" "three"])
(.mapMany #(Observable/toObservable (string-explode %)))
(.subscribe collect)
)
@collector
产生:
[\o \n \e \t \w \o \t \h \r \e \e]
我想说
(reset! collector [])
(->
(Observable/toObservable ["one" "two" "three"])
(.mapMany (comp Observable/toObservable string-explode))
(.subscribe collect)
)
但是,惊喜
clojure.lang.Compiler$CompilerException: java.lang.RuntimeException: Unable to find static field: toObservable in class rx.Observable, compiling:(NO_SOURCE_PATH:93:1)
Compiler.java:6380 clojure.lang.Compiler.analyze
Compiler.java:6322 clojure.lang.Compiler.analyze
Compiler.java:3624 clojure.lang.Compiler$InvokeExpr.parse
Compiler.java:6562 clojure.lang.Compiler.analyzeSeq
...
实际上,只是
Observable/toObservable
产生类似的异常.为什么Clojure在类似表达式中时可以找到Observable / toObservable
#(Observable/toObservable (string-explode %))
但不是像
(comp Observable/toObservable string-explode)
?