Created
December 26, 2016 11:29
-
-
Save noomorph/bf890bf60da771fa6f81321b1ab40e3f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createCombinationsIterable(sets) { | |
function len(arr) { | |
return arr.length; | |
} | |
function inc(digits, bases) { | |
var n = digits.length; | |
var ne = 0; | |
var sum = 0, mod = 0; | |
var excess = 1; | |
for (var j = n - 1; j >= 0 && excess === 1; j--) { | |
sum = digits[j] + excess; | |
excess = sum === bases[j] ? 1 : 0; | |
digits[j] = sum % bases[j]; | |
ne += excess; | |
} | |
return ne === n; | |
} | |
var n = len(sets); | |
var bases = sets.map(len); | |
return (_a = {}, | |
_a[Symbol.iterator] = function () { | |
var isNextStepLast = false; | |
var result = { done: false, value: null }; | |
var digits = new Array(n); | |
digits.fill(0); | |
return { | |
next: function () { | |
if (isNextStepLast) { | |
result.done = true; | |
result.value = null; | |
} | |
else { | |
var combination = new Array(n); | |
for (var i = 0; i < n; i++) { | |
combination[i] = sets[i][digits[i]]; | |
} | |
result.value = combination; | |
isNextStepLast = inc(digits, bases); | |
} | |
return result; | |
} | |
}; | |
}, | |
_a | |
); | |
var _a; | |
} | |
function* times(n, map) { | |
for (var i = 0; i < n; i++) yield map(i); | |
} | |
var features = [...times(4, i => [(i * 2 + 0), (i * 2 + 1)])]; | |
var iterator = createCombinationsIterable(features); | |
for (let combination of iterator) { | |
console.log(combination); | |
//console.log(...combination); | |
} | |
//var results = new Array(1 << 24); | |
//var i = 0; | |
//var itval; | |
//while (!(itval = iterator.next()).done) { | |
// print(itval.value); | |
// //results[i++] = itval.value; | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment