Created
June 7, 2019 16:28
-
-
Save kyontan/b9200087fe73f6784a9fd11b57a156ce to your computer and use it in GitHub Desktop.
lazy edition of Enumerable.product
This file contains hidden or 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
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