Last active
February 10, 2023 10:59
-
-
Save cybercase/db7dde901d7070c98c48 to your computer and use it in GitHub Desktop.
Python-like itertools.product function in javascript
This file contains 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 product() { | |
var args = Array.prototype.slice.call(arguments); // makes array from arguments | |
return args.reduce(function tl (accumulator, value) { | |
var tmp = []; | |
accumulator.forEach(function (a0) { | |
value.forEach(function (a1) { | |
tmp.push(a0.concat(a1)); | |
}); | |
}); | |
return tmp; | |
}, [[]]); | |
} | |
console.log(product([1], [2, 3], ['a', 'b'])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Generic version for Typescript: