function to efficiently check a change of value in a nested hashmap

Posted by zcaudate on Stack Overflow See other posts from Stack Overflow or by zcaudate
Published on 2012-12-04T06:21:02Z Indexed on 2012/12/04 11:08 UTC
Read the original article Hit count: 185

Filed under:

the motivation is for checking what has changed in a deeply nest map, kind of like a reverse of update-in.

This is a simple example:

(def p1 {:a {:a1 :1 :a2 :2}
         :b {:b1 :1 :b2 :2}})

(def p2 (update-in p1 [:a :a1] (constantly :updated))
;; => {:a {:a1 :updated :a2 :2}
;;     :b {:b1 :1 :b2 :2}}

(what-changed? p1 p2) 
;; => {:keys [:a :a1] :value :updated)

(what-changed? p2 p1) 
;; => {:keys [:a :a1] :value :2)

I'm hoping that because clojure maps are persistent data-structures, there may be a smart algorithm to figure this out by looking at the underlying structure as opposed to walking through the nested maps and comparing the difference.

© Stack Overflow or respective owner

Related posts about clojure