Skip to content

Instantly share code, notes, and snippets.

@michaelfeathers
Created April 18, 2012 12:34
Show Gist options
  • Save michaelfeathers/2413318 to your computer and use it in GitHub Desktop.
Save michaelfeathers/2413318 to your computer and use it in GitHub Desktop.
Each versus Reduce
/* On Array */
VALUE
rb_ary_each(ary)
VALUE ary;
{
long i;
for (i=0; i<RARRAY(ary)->len; i++) {
rb_yield(RARRAY(ary)->ptr[i]);
}
return ary;
}
/* On Enumerable */
static VALUE
enum_inject(argc, argv, obj)
int argc;
VALUE *argv, obj;
{
VALUE memo = Qundef;
if (rb_scan_args(argc, argv, "01", &memo) == 0)
memo = Qundef;
rb_iterate(rb_each, obj, inject_i, (VALUE)&memo);
if (memo == Qundef) return Qnil;
return memo;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment