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