Skip to content

Instantly share code, notes, and snippets.

@hsartoris-bard
Last active January 27, 2022 17:08
Show Gist options
  • Save hsartoris-bard/856d79d3a13f6cafaaa6e5079c76cd97 to your computer and use it in GitHub Desktop.
Save hsartoris-bard/856d79d3a13f6cafaaa6e5079c76cd97 to your computer and use it in GitHub Desktop.
Strip unchanged values from a deep-diff2 report
;; solution attempt for https://github.com/lambdaisland/deep-diff2/issues/13
(require '[clojure.walk :refer [postwalk]]
lambdaisland.deep-diff2.diff-impl
'[lambdaisland.deep-diff2 :as ddiff])
(import '[lambdaisland.deep_diff2.diff_impl Mismatch Deletion Insertion])
(defn diff-item?
"Checks if x is a Mismatch, Deletion, or Insertion"
[x]
(or (instance? Mismatch x)
(instance? Deletion x)
(instance? Insertion x)))
(def diff-or-col? #(or (coll? %) (diff-item? %)))
(defn remove-unchanged
"Postwalk diff, removing values that are unchanged"
[diff]
(postwalk
(fn [x]
(cond
(map-entry? x) (when (diff-or-coll? (val x)) x)
(map? x) (not-empty x)
(coll? x) (not-empty (into (empty x) (filter diff-or-coll?) x))
:else x))
diff))
;; example usage
(ddiff/pretty-print (remove-unchanged (ddiff/diff a b)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment