UNPKG

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