From e353a6f5f618466b6e24e648fa44d64b6a944118 Mon Sep 17 00:00:00 2001 From: Brent Verner Date: Fri, 18 Jul 2014 13:52:22 -0400 Subject: [PATCH 1/3] correct implementation of difffference; add symmetricDifference --- set.js | 13 ++++++++++++- test/set-test.js | 13 +++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) 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]) } From 096f35c9faa08834786d90678d4600c1f6c2bb16 Mon Sep 17 00:00:00 2001 From: dabreve Date: Fri, 18 Jul 2014 13:55:37 -0400 Subject: [PATCH 2/3] Bump version Bump version to 2.0.0 because of change in behaviour of difference --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ca5b831..c01d57f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "set", - "version": "1.1.1", + "version": "2.0.0", "description": "An implementation of Sets in JavaScript", "main": "set", "scripts": { From 55b2b3df945bfdb7f4e92b1a4fc877c5a9cf2127 Mon Sep 17 00:00:00 2001 From: dabreve Date: Sat, 19 Jul 2014 09:59:11 -0400 Subject: [PATCH 3/3] Revert version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c01d57f..ca5b831 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "set", - "version": "2.0.0", + "version": "1.1.1", "description": "An implementation of Sets in JavaScript", "main": "set", "scripts": {