mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Implement Enumerator::Lazy#eager [Feature #15901]
This commit is contained in:
parent
2a6457b5b7
commit
1d4bd229b8
3 changed files with 32 additions and 0 deletions
3
NEWS
3
NEWS
|
@ -102,6 +102,9 @@ Enumerator::
|
|||
|
||||
New method::
|
||||
|
||||
* Added Enumerator::Lazy#eager that generates a non-lazy enumerator
|
||||
from a lazy enumerator. [Feature #15901]
|
||||
|
||||
* Added Enumerator::Yielder#to_proc so that a Yielder object
|
||||
can be directly passed to another method as a block
|
||||
argument. [Feature #15618]
|
||||
|
|
21
enumerator.c
21
enumerator.c
|
@ -1893,6 +1893,26 @@ 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.
|
||||
*/
|
||||
|
||||
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)
|
||||
{
|
||||
|
@ -3741,6 +3761,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);
|
||||
|
|
|
@ -452,6 +452,14 @@ class TestLazyEnumerator < Test::Unit::TestCase
|
|||
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)
|
||||
|
|
Loading…
Reference in a new issue