Skip to content

Feature 4890 lazy enumerator #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Enumerable::Lazy#select added, exceptions when no block given, cleanup
  • Loading branch information
gregolsen committed Mar 3, 2012
commit e00738d81308de3e4c849c9e1b24aacfaa97ffb6
56 changes: 45 additions & 11 deletions enumerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
*/
VALUE rb_cEnumerator;
VALUE rb_cLazy;
static ID id_rewind, id_each;
static ID id_rewind, id_each, id_new, id_initialize, id_yield, id_call;
static VALUE sym_each;

VALUE rb_eStopIteration;
Expand Down Expand Up @@ -1192,46 +1192,73 @@ lazy_init_iterator(VALUE val, VALUE m, int argc, VALUE *argv)
if (rb_block_given_p()) {
return rb_yield(rb_ary_new3(2, m, val));
} else {
return rb_funcall(m, rb_intern("yield"), 1, val);
return rb_funcall(m, id_yield, 1, val);
}
}

static VALUE
lazy_init_block(VALUE val, VALUE m, int argc, VALUE *argv)
{
return rb_block_call(m, rb_intern("each"), 0, 0, lazy_init_iterator, val);
return rb_block_call(m, id_each, 0, 0, lazy_init_iterator, val);
}

static VALUE
lazy_initialize(int argc, VALUE *argv, VALUE obj)
{
VALUE generator = generator_allocate(rb_cGenerator);
rb_block_call(generator, rb_intern("initialize"), 0, 0, lazy_init_block, argv[0]);

rb_block_call(generator, id_initialize, 0, 0, lazy_init_block, argv[0]);
enumerator_init(obj, generator, sym_each, 0, 0);

return obj;
}

static VALUE
enumerable_lazy(VALUE obj)
{
return rb_funcall(rb_cLazy, rb_intern("new"), 1, obj);
return rb_funcall(rb_cLazy, id_new, 1, obj);
}

static VALUE
lazy_map_func(VALUE val, VALUE m, int argc, VALUE *argv)
{
VALUE result;
VALUE result = rb_funcall(rb_block_proc(), id_call, 1, rb_ary_entry(val, 1));

result = rb_funcall(m, rb_intern("call"), 1, rb_ary_entry(val, 1));

return rb_funcall(rb_ary_entry(val, 0), rb_intern("yield"), 1, result);
return rb_funcall(rb_ary_entry(val, 0), id_yield, 1, result);
}

static VALUE
lazy_map(VALUE obj)
{
VALUE map_proc = rb_block_proc();
return rb_block_call(rb_cLazy, rb_intern("new"), 1, &obj, lazy_map_func, map_proc);
if (!rb_block_given_p()) {
rb_raise(rb_eArgError, "tried to call lazy map without a block");
}

return rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_map_func, 0);
}


static VALUE
lazy_select_func(VALUE val, VALUE m, int argc, VALUE *argv)
{
VALUE element = rb_ary_entry(val, 1);
VALUE result = rb_funcall(rb_block_proc(), id_call, 1, element);

if (result) {
return rb_funcall(rb_ary_entry(val, 0), id_yield, 1, element);
} else {
return result;
}
}

static VALUE
lazy_select(VALUE obj)
{
if (!rb_block_given_p()) {
rb_raise(rb_eArgError, "tried to call lazy select without a block");
}

return rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_select_func, 0);
}

static VALUE
Expand Down Expand Up @@ -1270,7 +1297,10 @@ Init_Enumerator(void)
rb_define_method(rb_mEnumerable, "lazy", enumerable_lazy, 0);
rb_define_method(rb_cLazy, "initialize", lazy_initialize, -1);
rb_define_method(rb_cLazy, "map", lazy_map, 0);
rb_define_method(rb_cLazy, "select", lazy_select, 0);

rb_define_alias(rb_cLazy, "collect", "map");
rb_define_alias(rb_cLazy, "find_all", "select");

rb_eStopIteration = rb_define_class("StopIteration", rb_eIndexError);
rb_define_method(rb_eStopIteration, "result", stop_result, 0);
Expand All @@ -1292,6 +1322,10 @@ Init_Enumerator(void)

id_rewind = rb_intern("rewind");
id_each = rb_intern("each");
id_call = rb_intern("call");
id_yield = rb_intern("yield");
id_new = rb_intern("new");
id_initialize = rb_intern("initialize");
sym_each = ID2SYM(id_each);

rb_provide("enumerator.so"); /* for backward compatibility */
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy