UNPKG

393 BJavaScriptView Raw
1function* subsequences(x, n = -1) {
2 var X = x.length;
3 if (n >= X) {
4 if (n === X)
5 yield x;
6 return;
7 }
8 if (n === 0 || X === 0) {
9 yield [];
10 return;
11 }
12 var y = x.slice(0, -1);
13 yield* subsequences(y, n);
14 for (var s of subsequences(y, n - 1)) {
15 s.push(x[X - 1]);
16 yield s;
17 }
18}
19export default subsequences;