ruby-changes:25467
From: marcandre <ko1@a...>
Date: Wed, 7 Nov 2012 02:19:24 +0900 (JST)
Subject: [ruby-changes:25467] marcandRe: r37524 (trunk): * enumerator.c: Add support for lazy.drop.size
marcandre 2012-11-07 02:16:44 +0900 (Wed, 07 Nov 2012) New Revision: 37524 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=37524 Log: * enumerator.c: Add support for lazy.drop.size [Feature #6636] Modified files: trunk/enumerator.c trunk/test/ruby/test_lazy_enumerator.rb Index: enumerator.c =================================================================== --- enumerator.c (revision 37523) +++ enumerator.c (revision 37524) @@ -1674,6 +1674,19 @@ } 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) { NODE *memo = RNODE(args); @@ -1699,7 +1712,7 @@ 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 Index: test/ruby/test_lazy_enumerator.rb =================================================================== --- test/ruby/test_lazy_enumerator.rb (revision 37523) +++ test/ruby/test_lazy_enumerator.rb (revision 37524) @@ -341,5 +341,10 @@ 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/