Implement Enumerable::Lazy#to_enum and enum_for

This commit is contained in:
ksss
2016-11-30 11:02:02 +09:00
parent 2bb47addad
commit 8675975ba2
2 changed files with 15 additions and 0 deletions
+9
View File
@@ -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)
+6
View File
@@ -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