diff options
author | Stuart Halloway <stu@Orolo.local> | 2011-01-17 17:39:12 -0500 |
---|---|---|
committer | Stuart Halloway <stu@thinkrelevance.com> | 2011-01-28 10:09:39 -0500 |
commit | 76c1a58fc6d77151022a190d736327b1ade8ffa7 (patch) | |
tree | e03d6de6ba472f5e293a286f92e5b0a13af112c8 | |
parent | 553f4879cad019dd9dc1727165d8a41c216bd086 (diff) |
CLJ-719: call correct diff function for arrays as first argument
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
-rw-r--r-- | src/clj/clojure/data.clj | 38 | ||||
-rw-r--r-- | test/clojure/test_clojure/data.clj | 4 |
2 files changed, 24 insertions, 18 deletions
diff --git a/src/clj/clojure/data.clj b/src/clj/clojure/data.clj index 8f9a027d..27f430e6 100644 --- a/src/clj/clojure/data.clj +++ b/src/clj/clojure/data.clj @@ -12,6 +12,8 @@ clojure.data (:require [clojure.set :as set])) +(declare diff) + (defn- atom-diff "Internal helper for diff." [a b] @@ -28,7 +30,23 @@ (vec (repeat (apply max (keys m)) nil)) m))) -(declare diff) +(defn- diff-associative + "Diff associative things a and b, comparing only keys in ks." + [a b ks] + (reduce + (fn [diff1 diff2] + (map merge diff1 diff2)) + [nil nil nil] + (map + (fn [k] (map #(when % {k %}) (diff (get a k) (get b k)))) + ks))) + +(defn- diff-sequential + [a b] + (vec (map vectorize (diff-associative + (if (vector? a) a (vec a)) + (if (vector? b) b (vec b)) + (range (max (count a) (count b))))))) (defprotocol ^{:added "1.3"} EqualityPartition "Implementation detail. Subject to change." @@ -44,21 +62,10 @@ (extend Object Diff - {:diff-similar atom-diff} + {:diff-similar (fn [a b] ((if (.. a getClass isArray) diff-sequential atom-diff) a b))} EqualityPartition {:equality-partition (fn [x] (if (.. x getClass isArray) :sequential :atom))}) -(defn- diff-associative - "Diff associative things a and b, comparing only keys in ks." - [a b ks] - (reduce - (fn [diff1 diff2] - (map merge diff1 diff2)) - [nil nil nil] - (map - (fn [k] (map #(when % {k %}) (diff (get a k) (get b k)))) - ks))) - (extend-protocol EqualityPartition nil (equality-partition [x] :atom) @@ -88,10 +95,7 @@ java.util.List (diff-similar [a b] - (vec (map vectorize (diff-associative - (if (vector? a) a (vec a)) - (if (vector? b) b (vec b)) - (range (max (count a) (count b))))))) + (diff-sequential a b)) java.util.Map (diff-similar [a b] diff --git a/test/clojure/test_clojure/data.clj b/test/clojure/test_clojure/data.clj index 346983cf..9bab7666 100644 --- a/test/clojure/test_clojure/data.clj +++ b/test/clojure/test_clojure/data.clj @@ -24,6 +24,8 @@ [#{:a} #{:b} #{:c :d}] #{:a :c :d} #{:b :c :d} [nil nil {:a 1}] {:a 1} {:a 1} [{:a #{2}} {:a #{4}} {:a #{3}}] {:a #{2 3}} {:a #{3 4}} - [#{1} #{3} #{2}] (HashSet. [1 2]) (HashSet. [2 3]) + [#{1} #{3} #{2}] (HashSet. [1 2]) (HashSet. [2 3]) + [nil nil [1 2]] [1 2] (into-array [1 2]) + [nil nil [1 2]] (into-array [1 2]) [1 2] [{:a {:c [1]}} {:a {:c [0]}} {:a {:c [nil 2] :b 1}}] {:a {:b 1 :c [1 2]}} {:a {:b 1 :c [0 2]}})) |