2
我试图在clojurescript中构建出一个由原子支持的concept of a Cursor。游标是一种递归拉链机制,用于编辑不可变的嵌套关联数据结构。Clojure新手与协议奋斗
我在Clojure很新手,你能帮我发现我的错误吗?
(defprotocol Cursor
(refine [this path])
(set [this value])
(value [this]))
(defn- build-cursor* [state-atom paths]
(reify Cursor
(set [this value] (swap! state-atom (assoc-in @state-atom paths value)))
(refine [this path] (build-cursor* state-atom (conj paths path)))
(value [this] (get-in @state-atom paths))))
(defn build-cursor [state-atom]
(build-cursor* state-atom []))
(comment
(def s (atom {:a 42}))
(def c (build-cursor s))
(assert (= (value c) {:a 42}))
(set c {:a 43}) ;; WARNING: Wrong number of args (2) passed to quiescent-json-editor.core/set at line 1 <cljs repl>
(assert (= (value c) {:a 43}))
(def ca (refine c :a)) ;; WARNING: Wrong number of args (2) passed to quiescent-json-editor.core/refine at line 1 <cljs repl>
(assert (= (value ca) 43))
(set ca 44)
(assert (= (value ca) 43))
)
JIRA问题的assoc命令,在与空路径:http://dev.clojure.org/jira/browse/CLJ-1520 – lnmx 2014-10-06 17:02:05