1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* enum.c (enum_inject): it is now can work without block. you

have to specify two argument method name as the first argument.

* enum.c (Init_Enumerable): reduce is new alias to inject.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12297 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2007-05-18 16:10:46 +00:00
parent 62b3932c9c
commit 9eec640e9d
2 changed files with 80 additions and 6 deletions

View file

@ -1,3 +1,10 @@
Sat May 19 01:07:42 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* enum.c (enum_inject): it is now can work without block. you
have to specify two argument method name as the first argument.
* enum.c (Init_Enumerable): reduce is new alias to inject.
Sat May 19 01:05:33 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
* file.c (Init_File): method definition mismatch.

79
enum.c
View file

@ -347,8 +347,27 @@ inject_i(VALUE i, VALUE memo)
return Qnil;
}
static VALUE
inject_op_i(VALUE i, NODE *node)
{
VALUE memo = node->nd_rval;
if (RARRAY_PTR(memo)[0] == Qundef) {
RARRAY_PTR(memo)[0] = i;
}
else {
VALUE v = RARRAY_PTR(memo)[0];
RARRAY_PTR(memo)[1] = i;
RARRAY_PTR(memo)[0] = rb_funcall(v, node->nd_vid, 1, i);
}
return Qnil;
}
/*
* Document-method: inject
* call-seq:
* enum.inject(sym) => obj
* enum.inject(sym, initial) => obj
* enum.inject(initial) {| memo, obj | block } => obj
* enum.inject {| memo, obj | block } => obj
*
@ -358,6 +377,7 @@ inject_i(VALUE i, VALUE memo)
* first form lets you supply an initial value for <i>memo</i>. The
* second form uses the first element of the collection as a the
* initial value (and skips that element while iterating).
* See also <code>Enumerable#reduce</code>.
*
* # Sum some numbers
* (5..10).inject {|sum, n| sum + n } #=> 45
@ -378,18 +398,64 @@ inject_i(VALUE i, VALUE memo)
*
*/
/*
* Document-method: reduce
* call-seq:
* enum.reduce(sym) => obj
* enum.reduce(sym, initial) => obj
* enum.reduce {| memo, obj | block } => obj
* enum.reduce(initial) {| memo, obj | block } => obj
*
* Combines all elements of <i>enum</i> by applying a binary
* operation, specified by the block or metho-name symbol, for
* example, ary.reduce(:+) adds up all the elements. If no block is
* specified, the first argument is a method (or operator) name that
* takes two arguments. The second optional argument is the initial
* value. If a block is specified, the first optional value is the
* initial value.
*
* # Sum some numbers
* (5..10).reduce(:+) #=> 45
* # Same using a block
* (5..10).reduce {|sum, n| sum + n } #=> 45
* # Multiply some numbers
* (5..10).reduce(:*, 1) #=> 151200
* # Same using a block
* (5..10).reduce(1) {|product, n| product * n } #=> 151200
*
*/
static VALUE
enum_inject(int argc, VALUE *argv, VALUE obj)
{
VALUE memo, tmp;
VALUE memo, a1, a2, tmp;
NODE *node = 0;
if (rb_scan_args(argc, argv, "01", &tmp) == 0) {
switch (rb_scan_args(argc, argv, "02", &a1, &a2)) {
case 0:
memo = rb_ary_new3(2, Qundef, Qnil);
rb_block_call(obj, id_each, 0, 0, inject_i, memo);
break;
case 1:
if (rb_block_given_p()) {
memo = rb_ary_new3(2, a1, Qnil);
rb_block_call(obj, id_each, 0, 0, inject_i, memo);
}
else {
memo = rb_ary_new3(2, Qundef, Qnil);
node = rb_node_newnode(NODE_MEMO, rb_to_id(a1), memo, 0);
rb_block_call(obj, id_each, 0, 0, inject_op_i, (VALUE)node);
}
break;
case 2:
if (rb_block_given_p()) {
rb_warning("given block not used");
}
memo = rb_ary_new3(2, a2, Qnil);
node = rb_node_newnode(NODE_MEMO, rb_to_id(a1), memo, 0);
rb_block_call(obj, id_each, 0, 0, inject_op_i, (VALUE)node);
break;
}
else {
memo = rb_ary_new3(2, tmp, Qnil);
}
rb_block_call(obj, id_each, 0, 0, inject_i, (VALUE)memo);
tmp = RARRAY_PTR(memo)[0];
if (tmp == Qundef) return Qnil;
return tmp;
@ -1366,6 +1432,7 @@ Init_Enumerable(void)
rb_define_method(rb_mEnumerable,"collect", enum_collect, 0);
rb_define_method(rb_mEnumerable,"map", enum_collect, 0);
rb_define_method(rb_mEnumerable,"inject", enum_inject, -1);
rb_define_method(rb_mEnumerable,"reduce", enum_inject, -1);
rb_define_method(rb_mEnumerable,"partition", enum_partition, 0);
rb_define_method(rb_mEnumerable,"group_by", enum_group_by, 0);
rb_define_method(rb_mEnumerable,"first", enum_first, -1);