Skip to content

Instantly share code, notes, and snippets.

@kyontan
Created June 7, 2019 16:28
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 kyontan/b9200087fe73f6784a9fd11b57a156ce to your computer and use it in GitHub Desktop.
Save kyontan/b9200087fe73f6784a9fd11b57a156ce to your computer and use it in GitHub Desktop.
lazy edition of Enumerable.product
class LazyProduct
include Enumerable
def initialize(array)
@array = array
end
def self.from(array)
new(array)
end
def each(&block)
inner_each(rest_array_of_array: @array, &block)
nil
end
private def inner_each(args: [], rest_array_of_array:, &block)
if rest_array_of_array.nil? || rest_array_of_array.empty?
yield args
return
end
rest_array_of_array[0].each do |arg|
inner_each(args: args + [arg], rest_array_of_array: rest_array_of_array[1..-1], &block)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment