Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion set.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++){
Expand Down
13 changes: 13 additions & 0 deletions test/set-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand Down