Project

General

Profile

Feature #11773 ยป Enumerator_Lazy_grep_v.patch

spiritloose (Jiro Nishiguchi), 12/05/2015 07:30 AM

View differences:

enumerator.c
1423 1423
 *   e.lazy -> lazy_enumerator
1424 1424
 *
1425 1425
 * Returns a lazy enumerator, whose methods map/collect,
1426
 * flat_map/collect_concat, select/find_all, reject, grep, zip, take,
1426
 * flat_map/collect_concat, select/find_all, reject, grep, grep_v, zip, take,
1427 1427
 * take_while, drop, and drop_while enumerate values only on an
1428 1428
 * as-needed basis.  However, if a block is given to zip, values
1429 1429
 * are enumerated immediately.
......
1691 1691
}
1692 1692

  
1693 1693
static VALUE
1694
lazy_grep_v_func(RB_BLOCK_CALL_FUNC_ARGLIST(val, m))
1695
{
1696
    VALUE i = rb_enum_values_pack(argc - 1, argv + 1);
1697
    VALUE result = rb_funcall(m, id_eqq, 1, i);
1698

  
1699
    if (!RTEST(result)) {
1700
	rb_funcall(argv[0], id_yield, 1, i);
1701
    }
1702
    return Qnil;
1703
}
1704

  
1705
static VALUE
1706
lazy_grep_v_iter(RB_BLOCK_CALL_FUNC_ARGLIST(val, m))
1707
{
1708
    VALUE i = rb_enum_values_pack(argc - 1, argv + 1);
1709
    VALUE result = rb_funcall(m, id_eqq, 1, i);
1710

  
1711
    if (!RTEST(result)) {
1712
	rb_funcall(argv[0], id_yield, 1, rb_yield(i));
1713
    }
1714
    return Qnil;
1715
}
1716

  
1717
static VALUE
1718
lazy_grep_v(VALUE obj, VALUE pattern)
1719
{
1720
    return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
1721
					 rb_block_given_p() ?
1722
					 lazy_grep_v_iter : lazy_grep_v_func,
1723
					 pattern),
1724
			   rb_ary_new3(1, pattern), 0);
1725
}
1726

  
1727
static VALUE
1694 1728
call_next(VALUE obj)
1695 1729
{
1696 1730
    return rb_funcall(obj, id_next, 0);
......
2029 2063
    rb_define_method(rb_cLazy, "find_all", lazy_select, 0);
2030 2064
    rb_define_method(rb_cLazy, "reject", lazy_reject, 0);
2031 2065
    rb_define_method(rb_cLazy, "grep", lazy_grep, 1);
2066
    rb_define_method(rb_cLazy, "grep_v", lazy_grep_v, 1);
2032 2067
    rb_define_method(rb_cLazy, "zip", lazy_zip, -1);
2033 2068
    rb_define_method(rb_cLazy, "take", lazy_take, 1);
2034 2069
    rb_define_method(rb_cLazy, "take_while", lazy_take_while, 0);
test/ruby/test_lazy_enumerator.rb
197 197
                 e.lazy.grep(proc {|x| x == [2, "2"]}, &:join).force)
198 198
  end
199 199

  
200
  def test_grep_v
201
    a = Step.new('a'..'f')
202
    assert_equal('b', a.grep_v(/a/).first)
203
    assert_equal('f', a.current)
204
    assert_equal('a', a.lazy.grep_v(/c/).first)
205
    assert_equal('a', a.current)
206
    assert_equal(%w[b c d f], a.grep_v(proc {|x| /[aeiou]/ =~ x}))
207
    assert_equal(%w[b c d f], a.lazy.grep_v(proc {|x| /[aeiou]/ =~ x}).to_a)
208
  end
209

  
210
  def test_grep_v_with_block
211
    a = Step.new('a'..'f')
212
    assert_equal('B', a.grep_v(/a/) {|i| i.upcase}.first)
213
    assert_equal('B', a.lazy.grep_v(/a/) {|i| i.upcase}.first)
214
  end
215

  
216
  def test_grep_v_multiple_values
217
    e = Enumerator.new { |yielder|
218
      3.times { |i|
219
        yielder.yield(i, i.to_s)
220
      }
221
    }
222
    assert_equal([[0, "0"], [1, "1"]], e.grep_v(proc {|x| x == [2, "2"]}))
223
    assert_equal([[0, "0"], [1, "1"]], e.lazy.grep_v(proc {|x| x == [2, "2"]}).force)
224
    assert_equal(["00", "11"],
225
                 e.lazy.grep_v(proc {|x| x == [2, "2"]}, &:join).force)
226
  end
227

  
200 228
  def test_zip
201 229
    a = Step.new(1..3)
202 230
    assert_equal([1, "a"], a.zip("a".."c").first)