-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubsets.js
More file actions
45 lines (36 loc) · 857 Bytes
/
subsets.js
File metadata and controls
45 lines (36 loc) · 857 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
@author Jason Seminara
@date 2014-05-02
@description gets the power set (a set containing all possible subsets) of any array.
*/
Array.prototype.clone = function () {
return this.slice(0);
};
Array.prototype.powerSet = function () {
if (!this.length) return [[]];
/* pull off the head */
var head = this.shift();
/* recurse over the tail */
var tailsubsets = this.powerSet();
/* push in this item into a copy of each subset */
return tailsubsets.concat(
tailsubsets.clone().map(
function (i) {
i = i.clone();
i.push(head);
return i;
}
)
);
};
Array.prototype.isMember = function (b) {
return this.some(function (i) {
return this == i
}, b);
}
/*
//tests
console.log([6, 5, 't'].powerSet());
console.log(['a', 'b', 'c'].isMember('d'));
console.log([1, 2, 3].isMember(3));
*/