Project

General

Profile

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

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

View differences:

enumerator.c
1846 1846
    return lazy;
1847 1847
}
1848 1848

  
1849
static VALUE
1850
lazy_eager_size(VALUE self, VALUE args, VALUE eobj)
1851
{
1852
    return enum_size(self);
1853
}
1854

  
1855
/*
1856
 * call-seq:
1857
 *   lzy.eager -> enum
1858
 *
1859
 * Returns a non-lazy Enumerator converted from the lazy enumerator.
1860
 *
1861
 * This is useful where a normal Enumerable object needs to be
1862
 * generated while lazy operation is still desired to avoid creating
1863
 * intermediate arrays.
1864
 *
1865
 *   enum = huge_collection.lazy.flat_map(&:children).reject(&:disabled?).eager
1866
 *   enum.map {|x| ...}  # an array is returned
1867
 */
1868

  
1869
static VALUE
1870
lazy_eager(VALUE self)
1871
{
1872
    return enumerator_init(enumerator_allocate(rb_cEnumerator),
1873
                           self, sym_each, 0, 0, lazy_eager_size, Qnil);
1874
}
1875

  
1849 1876
static VALUE
1850 1877
lazyenum_yield(VALUE proc_entry, struct MEMO *result)
1851 1878
{
......
3548 3575
    rb_define_method(rb_cLazy, "initialize", lazy_initialize, -1);
3549 3576
    rb_define_method(rb_cLazy, "to_enum", lazy_to_enum, -1);
3550 3577
    rb_define_method(rb_cLazy, "enum_for", lazy_to_enum, -1);
3578
    rb_define_method(rb_cLazy, "eager", lazy_eager, 0);
3551 3579
    rb_define_method(rb_cLazy, "map", lazy_map, 0);
3552 3580
    rb_define_method(rb_cLazy, "collect", lazy_map, 0);
3553 3581
    rb_define_method(rb_cLazy, "flat_map", lazy_flat_map, 0);
test/ruby/test_lazy_enumerator.rb
452 452
EOS
453 453
  end
454 454

  
455
  def test_lazy_eager
456
    lazy = [1, 2, 3].lazy.map { |x| x * 2 }
457
    enum = lazy.eager
458
    assert_equal Enumerator, enum.class
459
    assert_equal 3, enum.size
460
    assert_equal [1, 2, 3], enum.map { |x| x / 2 }
461
  end
462

  
455 463
  def test_lazy_to_enum
456 464
    lazy = [1, 2, 3].lazy
457 465
    def lazy.foo(*args)