diff --git a/set.js b/set.js index 8070fd4..0329a93 100644 --- a/set.js +++ b/set.js @@ -101,11 +101,22 @@ Set.prototype.intersect = function(iset){ return oset } +Set.prototype.symmetricDifference = function(iset){ + // (A \ B) ∪ (B \ A) + return this.difference(iset).union(iset.difference(this)); +} + Set.prototype.difference = function(iset){ + /* From http://en.wikipedia.org/wiki/Set_theory + * Set difference of U and A, denoted U \ A, is the set of all + * members of U that are not members of A. The set difference + * {1,2,3} \ {2,3,4} is {1} , while, conversely, the set difference + * {2,3,4} \ {1,2,3} is {4} + */ var items = iset.get() , i = 0 , l = items.length - , oset = this.union(iset) + , oset = new Set(this.get()) , prop for(; i < l; i++){ diff --git a/test/set-test.js b/test/set-test.js index aaf30e0..a29a795 100644 --- a/test/set-test.js +++ b/test/set-test.js @@ -190,6 +190,19 @@ vows.describe('Set').addBatch({ assert.instanceOf(topic, Set) } + , "will get an array of [0,1]": function(topic){ + assert.deepEqual(topic.get(), [0,1]) + } + } + , "when taking the symmetric difference with a Set of [2,3,4,5,6,7]": { + topic: function(topic){ + return topic.symmetricDifference(new Set([2,3,4,5,6,7])) + } + + , "will give me a set": function(topic){ + assert.instanceOf(topic, Set) + } + , "will get an array of [0,1,5,6,7]": function(topic){ assert.deepEqual(topic.get(), [0,1,5,6,7]) }