Skip to content
Browse files

#710 diff mutable sets

Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
  • Loading branch information...
1 parent 9506ca6 commit 15c0f015f723f6c7e8ed3b3c88bb1ecfc01e7980 @stuarthalloway stuarthalloway committed Jan 9, 2011
Showing with 14 additions and 5 deletions.
  1. +11 −4 src/clj/clojure/data.clj
  2. +3 −1 test/clojure/test_clojure/data.clj
View
15 src/clj/clojure/data.clj
@@ -72,12 +72,19 @@
java.util.Map
(equality-partition [x] :map))
+(defn- as-set-value
+ [s]
+ (if (set? s) s (into #{} s)))
+
(extend-protocol Diff
java.util.Set
- (diff-similar [a b]
- [(not-empty (set/difference a b))
- (not-empty (set/difference b a))
- (not-empty (set/intersection a b))])
+ (diff-similar
+ [a b]
+ (let [aval (as-set-value a)
+ bval (as-set-value b)]
+ [(not-empty (set/difference aval bval))
+ (not-empty (set/difference bval aval))
+ (not-empty (set/intersection aval bval))]))
java.util.List
(diff-similar [a b]
View
4 test/clojure/test_clojure/data.clj
@@ -7,7 +7,8 @@
; You must not remove this notice, or any other, from this software.
(ns clojure.test-clojure.data
- (:use clojure.data clojure.test))
+ (:use clojure.data clojure.test)
+ (import java.util.HashSet))
(deftest diff-test
(are [d x y] (= d (diff x y))
@@ -23,5 +24,6 @@
[#{: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])
[{:a {:c [1]}} {:a {:c [0]}} {:a {:c [nil 2] :b 1}}] {:a {:b 1 :c [1 2]}} {:a {:b 1 :c [0 2]}}))

0 comments on commit 15c0f01

Please sign in to comment.
Something went wrong with that request. Please try again.