Skip to content

Kernel#lazy added #103

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 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 13 additions & 6 deletions enumerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,12 @@ generator_each(int argc, VALUE *argv, VALUE obj)
*
*/

static VALUE
stop_result(VALUE self)
{
return rb_attr_get(self, rb_intern("result"));
}

/* Lazy Enumerator methods */
static VALUE
lazy_init_iterator(VALUE val, VALUE m, int argc, VALUE *argv)
Expand Down Expand Up @@ -1244,6 +1250,12 @@ lazy_initialize(VALUE self, VALUE obj)
return self;
}

static VALUE
obj_to_lazy(VALUE obj)
{
return lazy_initialize(enumerator_allocate(rb_cLazy), obj);
}

/*
* call-seq:
* e.lazy -> lazy_enumerator
Expand Down Expand Up @@ -1367,17 +1379,12 @@ lazy_grep(VALUE obj, VALUE pattern)
return rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_grep_func, pattern);
}

static VALUE
stop_result(VALUE self)
{
return rb_attr_get(self, rb_intern("result"));
}

void
Init_Enumerator(void)
{
rb_define_method(rb_mKernel, "to_enum", obj_to_enum, -1);
rb_define_method(rb_mKernel, "enum_for", obj_to_enum, -1);
rb_define_method(rb_mKernel, "lazy", obj_to_lazy, 0);

rb_cEnumerator = rb_define_class("Enumerator", rb_cObject);
rb_include_module(rb_cEnumerator, rb_mEnumerable);
Expand Down
1 change: 1 addition & 0 deletions test/ruby/test_lazy_enumerator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def each(*args)
def test_initialize
assert_equal([1, 2, 3], [1, 2, 3].lazy.to_a)
assert_equal([1, 2, 3], Enumerable::Lazy.new([1, 2, 3]).to_a)
assert_equal([1, 2, 3], Step.new(1..3).lazy.to_a)
end

def test_each_args
Expand Down