How can I view a subset of the clojure tree (Hash Map) for changes?

I would like to follow the changes in different parts of the Clojure hash map (access via STM ref), which forms a fairly large tree, and with changes in those parts I would like to call some registered listeners. How to do this in Clojure, since I understand that add-watch only works with the whole link?

+3
source share
3 answers

I will also observe the whole tree and check the subsets with the entrance. Can you quickly check if a subtree has been changed using an identical one? test against the previous state. Sort of

(defn change-tester [tree path]
  (let [orig (get-in tree path)]
    (fn [tree]
      (not (identical? (get-in tree path) orig)))))

I don’t use observers often, so I don’t know the syntax, but you can somehow attach this function, I'm sure.

+1

Clojure , , .

:

  • , , . ( "get-in" ).
  • Marshall , , .
+2

Clojure , , , . "" , , . ( , , .)

, , , - , , , HashMap, Java, Clojure Map.

Java HashMap, "put" "putAll", , . HashMap HashMap, , sub HashMap , .

"WatchfulHashMap". HashMap, :

(def m (ref (WatchfulHashMap.)))

, .

+1

Source: https://habr.com/ru/post/1783119/


All Articles