* enumerator.c: Support for lazy.size
[Feature #6636] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37521 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
4437403426
commit
c8426ce840
23
enumerator.c
23
enumerator.c
@ -104,7 +104,7 @@
|
|||||||
*/
|
*/
|
||||||
VALUE rb_cEnumerator;
|
VALUE rb_cEnumerator;
|
||||||
VALUE rb_cLazy;
|
VALUE rb_cLazy;
|
||||||
static ID id_rewind, id_each, id_new, id_initialize, id_yield, id_call;
|
static ID id_rewind, id_each, id_new, id_initialize, id_yield, id_call, id_size;
|
||||||
static ID id_eqq, id_next, id_result, id_lazy, id_receiver, id_arguments, id_method;
|
static ID id_eqq, id_next, id_result, id_lazy, id_receiver, id_arguments, id_method;
|
||||||
static VALUE sym_each, sym_cycle;
|
static VALUE sym_each, sym_cycle;
|
||||||
|
|
||||||
@ -1239,6 +1239,23 @@ generator_each(int argc, VALUE *argv, VALUE obj)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Lazy Enumerator methods */
|
/* Lazy Enumerator methods */
|
||||||
|
static VALUE
|
||||||
|
lazy_receiver_size(VALUE self)
|
||||||
|
{
|
||||||
|
VALUE r = rb_check_funcall(rb_ivar_get(self, id_receiver), id_size, 0, 0);
|
||||||
|
return (r == Qundef) ? Qnil : r;
|
||||||
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
lazy_size(VALUE self)
|
||||||
|
{
|
||||||
|
struct enumerator *e = enumerator_ptr(self);
|
||||||
|
if (e->size_fn) {
|
||||||
|
return (*e->size_fn)(self);
|
||||||
|
}
|
||||||
|
return Qnil;
|
||||||
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
lazy_init_iterator(VALUE val, VALUE m, int argc, VALUE *argv)
|
lazy_init_iterator(VALUE val, VALUE m, int argc, VALUE *argv)
|
||||||
{
|
{
|
||||||
@ -1313,7 +1330,7 @@ lazy_initialize(int argc, VALUE *argv, VALUE self)
|
|||||||
rb_block_call(generator, id_initialize, 0, 0,
|
rb_block_call(generator, id_initialize, 0, 0,
|
||||||
(rb_block_given_p() ? lazy_init_block_i : lazy_init_block),
|
(rb_block_given_p() ? lazy_init_block_i : lazy_init_block),
|
||||||
obj);
|
obj);
|
||||||
enumerator_init(self, generator, meth, argc - offset, argv + offset, 0, Qnil);
|
enumerator_init(self, generator, meth, argc - offset, argv + offset, lazy_receiver_size, Qnil);
|
||||||
rb_ivar_set(self, id_receiver, obj);
|
rb_ivar_set(self, id_receiver, obj);
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
@ -1820,6 +1837,7 @@ InitVM_Enumerator(void)
|
|||||||
rb_define_method(rb_cLazy, "flat_map", lazy_flat_map, 0);
|
rb_define_method(rb_cLazy, "flat_map", lazy_flat_map, 0);
|
||||||
rb_define_method(rb_cLazy, "collect_concat", lazy_flat_map, 0);
|
rb_define_method(rb_cLazy, "collect_concat", lazy_flat_map, 0);
|
||||||
rb_define_method(rb_cLazy, "select", lazy_select, 0);
|
rb_define_method(rb_cLazy, "select", lazy_select, 0);
|
||||||
|
rb_define_method(rb_cLazy, "size", lazy_size, 0);
|
||||||
rb_define_method(rb_cLazy, "find_all", lazy_select, 0);
|
rb_define_method(rb_cLazy, "find_all", lazy_select, 0);
|
||||||
rb_define_method(rb_cLazy, "reject", lazy_reject, 0);
|
rb_define_method(rb_cLazy, "reject", lazy_reject, 0);
|
||||||
rb_define_method(rb_cLazy, "grep", lazy_grep, 1);
|
rb_define_method(rb_cLazy, "grep", lazy_grep, 1);
|
||||||
@ -1860,6 +1878,7 @@ Init_Enumerator(void)
|
|||||||
id_rewind = rb_intern("rewind");
|
id_rewind = rb_intern("rewind");
|
||||||
id_each = rb_intern("each");
|
id_each = rb_intern("each");
|
||||||
id_call = rb_intern("call");
|
id_call = rb_intern("call");
|
||||||
|
id_size = rb_intern("size");
|
||||||
id_yield = rb_intern("yield");
|
id_yield = rb_intern("yield");
|
||||||
id_new = rb_intern("new");
|
id_new = rb_intern("new");
|
||||||
id_initialize = rb_intern("initialize");
|
id_initialize = rb_intern("initialize");
|
||||||
|
@ -323,4 +323,10 @@ class TestLazyEnumerator < Test::Unit::TestCase
|
|||||||
#<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: 1..10>:map>:collect>:flat_map>:collect_concat>:select>:find_all>:reject>:grep(1)>:zip("a".."c")>:take(10)>:take_while>:drop(3)>:drop_while>:cycle(3)>
|
#<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: 1..10>:map>:collect>:flat_map>:collect_concat>:select>:find_all>:reject>:grep(1)>:zip("a".."c")>:take(10)>:take_while>:drop(3)>:drop_while>:cycle(3)>
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_size
|
||||||
|
lazy = [1, 2, 3].lazy
|
||||||
|
assert_equal 3, lazy.size
|
||||||
|
assert_equal 42, Enumerator.new(42){}.lazy.size
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user