mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* enumerator.c: Add support for lazy.drop.size
[Feature #6636] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37524 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
9aafa954aa
commit
7a31096255
2 changed files with 19 additions and 1 deletions
15
enumerator.c
15
enumerator.c
|
@ -1673,6 +1673,19 @@ lazy_take_while(VALUE obj)
|
|||
Qnil, 0);
|
||||
}
|
||||
|
||||
static VALUE
|
||||
lazy_drop_size(VALUE lazy) {
|
||||
long len = NUM2LONG(RARRAY_PTR(rb_ivar_get(lazy, id_arguments))[0]);
|
||||
VALUE receiver = lazy_receiver_size(lazy);
|
||||
if (NIL_P(receiver))
|
||||
return receiver;
|
||||
if (FIXNUM_P(receiver)) {
|
||||
len = FIX2LONG(receiver) - len;
|
||||
return LONG2FIX(len < 0 ? 0 : len);
|
||||
}
|
||||
return rb_funcall(receiver, '-', 1, LONG2NUM(len));
|
||||
}
|
||||
|
||||
static VALUE
|
||||
lazy_drop_func(VALUE val, VALUE args, int argc, VALUE *argv)
|
||||
{
|
||||
|
@ -1699,7 +1712,7 @@ lazy_drop(VALUE obj, VALUE n)
|
|||
memo = NEW_MEMO(0, 0, len);
|
||||
return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
|
||||
lazy_drop_func, (VALUE) memo),
|
||||
rb_ary_new3(1, n), 0);
|
||||
rb_ary_new3(1, n), lazy_drop_size);
|
||||
}
|
||||
|
||||
static VALUE
|
||||
|
|
|
@ -341,5 +341,10 @@ EOS
|
|||
assert_equal 3, lazy.take(4).size
|
||||
assert_equal 4, loop.lazy.take(4).size
|
||||
assert_equal nil, lazy.select{}.take(4).size
|
||||
|
||||
assert_equal 1, lazy.drop(2).size
|
||||
assert_equal 0, lazy.drop(4).size
|
||||
assert_equal Float::INFINITY, loop.lazy.drop(4).size
|
||||
assert_equal nil, lazy.select{}.drop(4).size
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue