Created
July 13, 2012 19:00
-
-
Save isaksky/3106701 to your computer and use it in GitHub Desktop.
cartesian product
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
def ghetto_cartesian_product colls | |
if colls.empty? | |
[[]] | |
else | |
colls.first.reduce([]) do |memo, e| | |
rest = colls.drop 1 | |
tails = ghetto_cartesian_product(rest).map do |tail| | |
[e].concat tail | |
end | |
memo.concat tails | |
end | |
end | |
end | |
# so good | |
def lazy_cartesian_product colls | |
colls.first.enum_for :product, *colls.drop(1) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment