Permalink
Browse files

Adding Set#complement() method with tests and docs. [ionfish]

  • Loading branch information...
jcoglan committed Aug 8, 2008
1 parent d9e0a9d commit ad9d4966832486a3139aa6aff3c1f3a1fba573a4
Showing with 13 additions and 0 deletions.
  1. +4 −0 site/src/pages/set.haml
  2. +8 −0 source/set.js
  3. +1 −0 test/test.html
View
@@ -77,6 +77,10 @@
Removes all members from the set.
+ h3. @complement(other)@
+
+ Returns a new set containing all the members of @other@ that are not in the receiver.
+
h3. @contains(item)@
Returns @true@ iff the set contains @item@.
View
@@ -50,6 +50,14 @@ JS.Set = new JS.Class({
this.length = this.size = this._members.length;
},
+ complement: function(other) {
+ var set = new this.klass;
+ this.klass.forEach(other, function(item) {
+ if (!this.contains(item)) set.add(item);
+ }, this);
+ return set;
+ },
+
contains: function(item) {
return this._indexOf(item) != -1;
},
View
@@ -1240,6 +1240,7 @@
assertEnumEqual([5, sorted], inner.entries());
assertEnumEqual([4,9,8], new JS.Set([8,2,9,4,'foo']).difference(['foo',2]));
+ assertEnumEqual([0,1,7], new JS.SortedSet([3,9,4,6]).complement([1,0,3,7,6,9,4]));
var sets = a.u(b).divide(function(x) { return x % 3 });
assert(sets instanceof JS.Set);

0 comments on commit ad9d496

Please sign in to comment.