Last active
December 19, 2015 23:59
-
-
Save svdmitrij/0dfdb213e06eb5dcbb80 to your computer and use it in GitHub Desktop.
Cartesian product coffeescript
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
cartProd = (paramArray) -> | |
addTo = (curr, args) -> | |
rest = args.slice(1) | |
last = not rest.length | |
result = [] | |
i = 0 | |
while i < args[0].length | |
copy = curr.slice() | |
copy.push args[0][i] | |
if last | |
result.push copy | |
else | |
result = result.concat(addTo(copy, rest)) | |
i++ | |
result | |
addTo [], Array::slice.call(arguments_) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment