From: nobu.nokada@... Date: 2004-05-26T05:24:59+09:00 Subject: Re: [RCR] Enumerable#group_by Hi, At Wed, 26 May 2004 02:16:27 +0900, Michael Neumann wrote in [ruby-talk:101382]: > I propose the addition of Enumerable#group_by which is a generalization > of Enumerable#partition. I think it has been proposed also as Enumerable#classify. Here is a patch I made at that time. Index: enum.c =================================================================== RCS file: /cvs/ruby/src/ruby/enum.c,v retrieving revision 1.45 diff -U2 -p -d -r1.45 enum.c --- enum.c 14 Apr 2004 04:06:24 -0000 1.45 +++ enum.c 21 Apr 2004 08:37:31 -0000 @@ -362,4 +362,37 @@ enum_partition(obj) } +static VALUE +classify_i(i, hash) + VALUE i, hash; +{ + VALUE k = rb_yield(i); + VALUE ary = rb_hash_aref(hash, k); + + if (NIL_P(ary)) { + rb_hash_aset(hash, k, ary = rb_ary_new()); + } + rb_ary_push(ary, i); + + return Qnil; +} + +/* + * call-seq: + * enum.classify {| x | block } => hash + * + * Returns a hash containing the items in enum classified. + */ + +static VALUE +enum_classify(obj) + VALUE obj; +{ + VALUE hash = rb_hash_new(); + + rb_iterate(rb_each, obj, classify_i, hash); + + return hash; +} + /* * call-seq: @@ -930,4 +963,5 @@ Init_Enumerable() rb_define_method(rb_mEnumerable,"inject", enum_inject, -1); rb_define_method(rb_mEnumerable,"partition", enum_partition, 0); + rb_define_method(rb_mEnumerable,"classify", enum_classify, 0); rb_define_method(rb_mEnumerable,"all?", enum_all, 0); rb_define_method(rb_mEnumerable,"any?", enum_any, 0); -- Nobu Nakada