How to compare data structures in clojure and highlight the differences

Is there a good way to print differences in clojure data structures? For example, in Perl there is Test :: Differences , which helps a lot.

+9
clojure testing
source share
5 answers

What I was really looking for was difform . Using clojure.data/diff nice, but it doesn't work in unit test, where larger structures are compared. Here is an example where data/diff does not work as well as in my opinion:

 (defn example [] (let [data [{:foo 1} {:value [{:bar 2}]}]] (diff data [{:value [{:bar 2}]}]) (difform data [{:value [{:bar 2}]}]))) ;; => diff output ;; => [[{:foo 1} {:value [{:bar 2}]}] [{:value [{:bar 2}]}] nil] ;; => difform output [{: - foo 1} {: value [{:bar 2}]}] ;; => nil 
+1
source share

Take a look at clojure.data/diff: http://clojure.imtqy.com/clojure/clojure.data-api.html#clojure.data/diff

Examples:

 async-demo.core> (use 'clojure.data) nil async-demo.core> (diff {:a 2 :b 4} {:a 2}) ({:b 4} nil {:a 2}) async-demo.core> (diff [1 2 3 4] [1 2 6 7]) [[nil nil 3 4] [nil nil 6 7] [1 2]] async-demo.core> (diff #{"one" "two" "three"} #{"one" "fourty-four"}) [#{"two" "three"} #{"fourty-four"} #{"one"}] 
+10
source share

You can also visually distinguish between two data structures using the gui-diff library.

(gui-diff {:a 1} {:a 2}) laid out in the appropriate GUI program corresponding to the OS to separate two, potentially very large data structures.

+4
source share

Differ is a recent library that seems to do a nice job:

 (def person-map {:name "Robin" :age 25 :sex :male :phone {:home 99999999 :work 12121212}) (def person-diff (differ/diff person-map {:name "Robin Heggelund Hansen" :age 26 :phone {:home 99999999}) ;; person-diff will now be [{:name "Robin Heggelund Hansen" ;; :age 26} ;; {:sex 0 ;; :phone {:work 0}] 

EDITED: fix Difference between repo url changed from gitlab to github

0
source share

The bizarre difference I saw is the deep difference

It produces a nice colorful differential like this

enter image description here

It also mentions a good editscript library that produces diff as an array of patches.

0
source share

All Articles