Created
April 18, 2012 12:34
-
-
Save michaelfeathers/2413318 to your computer and use it in GitHub Desktop.
Each versus Reduce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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