Skip to content

Instantly share code, notes, and snippets.

@sorah sorah/a.diff
Created May 10, 2011

Embed
What would you like to do?
diff --git enumerator.c enumerator.c
index 47a5c7e..2da0b14 100644
--- enumerator.c
+++ enumerator.c
@@ -794,6 +794,26 @@ enumerator_inspect(VALUE obj)
return rb_exec_recursive(inspect_enumerator, obj, 0);
}
+static VALUE
+enumerator_defer_ii(VALUE i, VALUE y, int c, VALUE *v)
+{
+ return rb_funcall(y, rb_intern("<<"), 1, rb_yield_values2(c, v));
+}
+
+static VALUE
+enumerator_defer_i(VALUE y, VALUE i, int c, VALUE *v)
+{
+ return rb_block_call(i, id_each, 0, 0, enumerator_defer_ii, y);
+}
+
+static VALUE
+enumerator_defer(VALUE obj)
+{
+ VALUE ret = rb_obj_alloc(rb_cEnumerator);
+ rb_block_call(ret, rb_intern("initialize"), 0, 0, enumerator_defer_i, obj);
+ return ret;
+}
+
/*
* Yielder
*/
@@ -1099,6 +1119,7 @@ Init_Enumerator(void)
rb_define_method(rb_cEnumerator, "feed", enumerator_feed, 1);
rb_define_method(rb_cEnumerator, "rewind", enumerator_rewind, 0);
rb_define_method(rb_cEnumerator, "inspect", enumerator_inspect, 0);
+ rb_define_method(rb_cEnumerator, "defer", enumerator_defer, 0);
rb_eStopIteration = rb_define_class("StopIteration", rb_eIndexError);
rb_define_method(rb_eStopIteration, "result", stop_result, 0);
a = [1,2,3].map.defer{|x| x*2 }
p a
a.each_with_index do |x,i|
p [x,i]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.