From 6ea2c7de6aa69116555db067cd7a7e1aa9635f06 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Wed, 5 Jun 2019 20:39:21 +0900 Subject: [PATCH] Implement Enumerator::Lazy#eager --- enumerator.c | 28 ++++++++++++++++++++++++++++ test/ruby/test_lazy_enumerator.rb | 8 ++++++++ 2 files changed, 36 insertions(+) diff --git a/enumerator.c b/enumerator.c index 808ab67de8..78c2cebcaa 100644 --- a/enumerator.c +++ b/enumerator.c @@ -1846,6 +1846,33 @@ lazy_to_enum(int argc, VALUE *argv, VALUE self) 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) { @@ -3548,6 +3575,7 @@ InitVM_Enumerator(void) 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); diff --git a/test/ruby/test_lazy_enumerator.rb b/test/ruby/test_lazy_enumerator.rb index d42c8d3261..46bdc521ab 100644 --- a/test/ruby/test_lazy_enumerator.rb +++ b/test/ruby/test_lazy_enumerator.rb @@ -452,6 +452,14 @@ def test_inspect 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) -- 2.21.0