Reputation: 8069
So, I don't quite get Ruby 1.9 's changes on enumerators, but what I'd like is a lazy filtering/mapping on a collection, so for example how can I create a lazy enumerator that would be the equivalent of this ...
(1..100).find_all{|x| x % 2 == 1}.map{|x| x * 2}.find_all{|x| x % 3 == 0}
I tried passing a lambda to #enum_for, but it doesn't work on my machine.
Upvotes: 1
Views: 1226
Reputation: 370162
Sadly, you can't do this with enum_for. You have to use something like this for lazy enumerables:
class LazyEnum
include Enumerable
attr_accessor :enum, :operations
def initialize enum
@enum = enum
@operations = []
end
def each
enum.each do |x|
filtered = false
operations.each do |type, op|
if type == :filter
unless op[x]
filtered = true
break
end
else
x = op[x]
end
end
yield x unless filtered
end
end
def map! &blk
@operations << [:transform, blk]
self
end
def select! &blk
@operations << [:filter, blk]
self
end
def reject!
select! {|x| !(yield x)}
end
def dup
LazyEnum.new self
end
def map &blk
dup.map! &blk
end
def select &blk
dup.select! &blk
end
def reject &blk
dup.reject! &blk
end
end
LazyEnum.new(1..100).select{|x| x % 2 == 1}.map{|x| x * 2}.select{|x| x % 3 == 0}
#=> #<LazyEnum:0x7f7e11582000 @enum=#<LazyEnum:0x7f7e115820a0 @enum=#<LazyEnum:0x7f7e11582140 @enum=#<LazyEnum:0x7f7e115822d0 @enum=1..100, @operations=[]>, @operations=[[:filter, #<Proc:0x00007f7e11584058@(irb):348>]]>, @operations=[[:transform, #<Proc:0x00007f7e11583a90@(irb):348>]]>, @operations=[[:filter, #<Proc:0x00007f7e115823c0@(irb):348>]]>
irb(main):349:0> LazyEnum.new(1..100).select{|x| x % 2 == 1}.map{|x| x * 2}.select{|x| x % 3 == 0}.to_a
#=> [6, 18, 30, 42, 54, 66, 78, 90, 102, 114, 126, 138, 150, 162, 174, 186, 198]
Upvotes: 0
Reputation: 54762
You can find implementations of lazy_select and lazy_map in this blog. You will just have to extend the Enumerator module by those two methods. Then you should be able to use
(1..100).lazy_select{|x| x % 2 == 1}.lazy_map{|x| x * 2}.lazy_select{|x| x % 3 == 0}
Upvotes: 3