Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.