Skip to content

Instantly share code, notes, and snippets.

@isaksky
Created July 13, 2012 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaksky/3106701 to your computer and use it in GitHub Desktop.
Save isaksky/3106701 to your computer and use it in GitHub Desktop.
cartesian product
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