Project

General

Profile

Feature #15901 ยป 0001-Implement-Enumerator-Lazy-eager.patch

knu (Akinori MUSHA), 06/05/2019 11:53 AM

View differences:

enumerator.c
return lazy;
}
static VALUE
lazy_eager_size(VALUE self, VALUE args, VALUE eobj)
{
return enum_size(self);
}
/*
* call-seq:
* lzy.eager -> enum
*
* Returns a non-lazy Enumerator converted from the lazy enumerator.
*
* This is useful where a normal Enumerable object needs to be
* generated while lazy operation is still desired to avoid creating
* intermediate arrays.
*
* enum = huge_collection.lazy.flat_map(&:children).reject(&:disabled?).eager
* enum.map {|x| ...} # an array is returned
*/
static VALUE
lazy_eager(VALUE self)
{
return enumerator_init(enumerator_allocate(rb_cEnumerator),
self, sym_each, 0, 0, lazy_eager_size, Qnil);
}
static VALUE
lazyenum_yield(VALUE proc_entry, struct MEMO *result)
{
......
rb_define_method(rb_cLazy, "initialize", lazy_initialize, -1);
rb_define_method(rb_cLazy, "to_enum", lazy_to_enum, -1);
rb_define_method(rb_cLazy, "enum_for", lazy_to_enum, -1);
rb_define_method(rb_cLazy, "eager", lazy_eager, 0);
rb_define_method(rb_cLazy, "map", lazy_map, 0);
rb_define_method(rb_cLazy, "collect", lazy_map, 0);
rb_define_method(rb_cLazy, "flat_map", lazy_flat_map, 0);
test/ruby/test_lazy_enumerator.rb
EOS
end
def test_lazy_eager
lazy = [1, 2, 3].lazy.map { |x| x * 2 }
enum = lazy.eager
assert_equal Enumerator, enum.class
assert_equal 3, enum.size
assert_equal [1, 2, 3], enum.map { |x| x / 2 }
end
def test_lazy_to_enum
lazy = [1, 2, 3].lazy
def lazy.foo(*args)
    (1-1/1)