Skip to content

Instantly share code, notes, and snippets.

@svdmitrij
Last active December 19, 2015 23:59
Show Gist options
  • Save svdmitrij/0dfdb213e06eb5dcbb80 to your computer and use it in GitHub Desktop.
Save svdmitrij/0dfdb213e06eb5dcbb80 to your computer and use it in GitHub Desktop.
Cartesian product coffeescript
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