mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Merge pull request #3305 from ksss/lazy-to_enum
Implement Enumerable::Lazy#to_enum and enum_for
This commit is contained in:
@@ -40,6 +40,15 @@ module Enumerable
|
||||
}
|
||||
end
|
||||
|
||||
def to_enum(meth=:each, *args, &block)
|
||||
lz = Lazy.new(self, &block)
|
||||
lz.obj = self
|
||||
lz.meth = meth
|
||||
lz.args = args
|
||||
lz
|
||||
end
|
||||
alias enum_for to_enum
|
||||
|
||||
def map(&block)
|
||||
Lazy.new(self){|yielder, val|
|
||||
yielder << block.call(val)
|
||||
|
||||
@@ -40,6 +40,12 @@ assert("Enumerable::Lazy laziness") do
|
||||
assert_equal [10,20], a.b
|
||||
end
|
||||
|
||||
assert("Enumrable::Lazy#to_enum") do
|
||||
lazy_enum = (0..Float::INFINITY).lazy.to_enum(:each_slice, 2)
|
||||
assert_kind_of Enumerable::Lazy, lazy_enum
|
||||
assert_equal [0*1, 2*3, 4*5, 6*7], lazy_enum.map { |a| a.first * a.last }.first(4)
|
||||
end
|
||||
|
||||
assert("Enumerable::Lazy#zip with cycle") do
|
||||
e1 = [1, 2, 3].cycle
|
||||
e2 = [:a, :b].cycle
|
||||
|
||||
Reference in New Issue
Block a user