mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* enum.c (enum_inject): use the first iterated element as the
initial value when omitted. * enum.c (inject_i): ditto. * enum.c (Init_Enumerable): Enumerable#inject now takes variable count arguments. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2215 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
b4e74d4038
commit
cecb9cae01
2 changed files with 34 additions and 7 deletions
10
ChangeLog
10
ChangeLog
|
@ -8,6 +8,16 @@ Sat Mar 16 22:43:53 2002 WATANABE Hirofumi <eban@ruby-lang.org>
|
|||
|
||||
* missing/fileblocks.c: add for autoconf.
|
||||
|
||||
Sat Mar 16 09:04:58 2002 Koji Arai <JCA02266@nifty.ne.jp>
|
||||
|
||||
* enum.c (enum_inject): use the first iterated element as the
|
||||
initial value when omitted.
|
||||
|
||||
* enum.c (inject_i): ditto.
|
||||
|
||||
* enum.c (Init_Enumerable): Enumerable#inject now takes variable
|
||||
count arguments.
|
||||
|
||||
Fri Mar 15 19:47:31 2002 Nobuyoshi Nakada <nobu.nakada@nifty.ne.jp>
|
||||
|
||||
* win32/win32.c (StartSockets): remove duplicated lines.
|
||||
|
|
31
enum.c
31
enum.c
|
@ -180,20 +180,37 @@ enum_collect(obj)
|
|||
}
|
||||
|
||||
static VALUE
|
||||
inject_i(i, np)
|
||||
inject_i(i, memo)
|
||||
VALUE i;
|
||||
VALUE *np;
|
||||
NODE *memo;
|
||||
{
|
||||
*np = rb_yield(rb_assoc_new(*np, i));
|
||||
if (memo->u2.value) {
|
||||
memo->u2.value = Qfalse;
|
||||
memo->u1.value = i;
|
||||
}
|
||||
else
|
||||
memo->u1.value = rb_yield(rb_assoc_new(memo->u1.value, i));
|
||||
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE
|
||||
enum_inject(obj, n)
|
||||
VALUE obj, n;
|
||||
enum_inject(argc, argv, obj)
|
||||
int argc;
|
||||
VALUE *argv, obj;
|
||||
{
|
||||
rb_iterate(rb_each, obj, inject_i, (VALUE)&n);
|
||||
NODE *memo;
|
||||
VALUE n;
|
||||
|
||||
if (rb_scan_args(argc, argv, "01", &n) == 1)
|
||||
memo = rb_node_newnode(NODE_MEMO, n, Qfalse, 0);
|
||||
else
|
||||
memo = rb_node_newnode(NODE_MEMO, Qnil, Qtrue, 0);
|
||||
|
||||
rb_iterate(rb_each, obj, inject_i, (VALUE)memo);
|
||||
n = memo->u1.value;
|
||||
|
||||
rb_gc_force_recycle((VALUE)memo);
|
||||
return n;
|
||||
}
|
||||
|
||||
|
@ -448,7 +465,7 @@ Init_Enumerable()
|
|||
rb_define_method(rb_mEnumerable,"reject", enum_reject, 0);
|
||||
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,"inject", enum_inject, -1);
|
||||
rb_define_method(rb_mEnumerable,"all?", enum_all, 0);
|
||||
rb_define_method(rb_mEnumerable,"any?", enum_any, 0);
|
||||
rb_define_method(rb_mEnumerable,"min", enum_min, 0);
|
||||
|
|
Loading…
Add table
Reference in a new issue