Permalink
Browse files

Implement Enumerable::Lazy#to_enum and enum_for

  • Loading branch information...
ksss committed Nov 30, 2016
1 parent 2bb47ad commit 8675975ba212be389624ed165c287e9988e916db
Showing with 15 additions and 0 deletions.
  1. +9 −0 mrbgems/mruby-enum-lazy/mrblib/lazy.rb
  2. +6 −0 mrbgems/mruby-enum-lazy/test/lazy.rb
@@ -40,6 +40,15 @@ def initialize(obj, &block)
}
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 @@ def a.b(b=nil)
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

0 comments on commit 8675975

Please sign in to comment.