clojure.data/diff recursively compares two values and reports what belongs only to the first value, only to the second, and to both. Unlike =, which returns only a boolean, diff describes how nested data differs. section 6.7 section 6.0
Call diff with exactly two values:
- Returns [only-in-a only-in-b common] when the inputs have compatible types and share some structure. section 6.7
- Returns [a b nil] when the values share nothing or have incompatible types. section 6.7
- Compares maps, lists, vectors, sets, Java arrays, and scalar values, using compatibility rules for each category. section 6.7
- Raises ArityException unless exactly two arguments are supplied; deeply nested structures can also exhaust the stack. section 6.7
Comparing nested configuration
(diff {:a "1" :b "2"} {:b "2" :c "4"}) ;; ({:a "1"} {:c "4"} {:b "2"}) (diff [1 {:a [1 2]}] [1 {:a [1 3]}]) ;; [[nil {:a [nil 2]}] ;; [nil {:a [nil 3]}] ;; [1 {:a [1]}]] The third element preserves the common nested structure, while the first two isolate each side's changes.
Turning a diff into paths
The first result can be walked recursively to locate changed keys. For two service configurations, diff-to-path produces paths such as [:profiles :dev :dependencies :dep8] and [:profiles :uberjar :aot], which can then guide get-in or targeted updates.
