Compare recursively two Clojure data structures a and b.

Comparison is updated as you type…

(def a {:only-in-a 1
        :both-in-a-and-b 3}) 
(def b {:only-in-b 2
        :both-in-a-and-b 3}) 

Full comparison:

(def full-comparison (clojure.data/diff a b)) 

Things only in a:

(first full-comparison)

Things only in b:

(second full-comparison)

Things in both a and b:

(last full-comparison)

Comparison rules:

  • For equal a and b, return [nil nil a].
  • Maps are subdiffed where keys match and values differ.
  • Sets are never subdiffed.
  • All sequential things are treated as associative collections by their indexes, with results returned as vectors.
  • Everything else (including strings!) is treated as an atom and compared for equality.

Explore more examples on ClojureDocs.