Hello Akira,

On 2010/12/14 18:41, Tanaka Akira wrote:
> 2010/12/12 "Martin J. Dürst"<duerst / it.aoyama.ac.jp>:
>>
>> There are already such methods. At least group_by. Your proposal seems to be
>> a very special way to convert an enumerable to a hash.
>
> Enumerable#categorize is more general than Enumerable#group_by.

I know. I think it's way too general and difficult to understand.

>> I think I get the idea of how this is supposed to work. But I'm not sure
>> what the actual use cases would be. Can you give some?
>
> I see.
>
> Assume a table as follows.
>
>    ary = [
>      ["matz", "Yukihiro Matsumoto"],
>      ["nobu", "Nobuyoshi Nakada"],
>      ["akr", "Tanaka Akira"],
>      ["usa", "Usaku NAKAMURA"],
>      ["naruse", "NARUSE, Yui"],
>      ["ko1", "SASADA Koichi"]
>    ]
>
> Enumerable#categorize can be used to generate a hash
> from the right elements to left elements.
> (and left elements to right elements too.)
>
>    pp ary.categorize {|e| [e[1], e[0]] }
>    #=>
>    {"Yukihiro Matsumoto"=>["matz"],
>     "Nobuyoshi Nakada"=>["nobu"],
>     "Tanaka Akira"=>["akr"],
>     "Usaku NAKAMURA"=>["usa"],
>     "NARUSE, Yui"=>["naruse"],
>     "SASADA Koichi"=>["ko1"]}
>
> Since Enumerable#group_by cannot specify hash values,
> it cannot do it.

This is one example. I don't deny that such examples exist. But I think 
they are not so frequent, and quite varied (i.e. there are many examples 
where one needs "almost something like this, but not quite exactly the 
same).

I think that unless we can boil down your proposal to something simple 
that the average advanced Ruby programmer can understand and use without 
having to look it up or try it out in irb all the time, we have to 
invest some more time to find the right method.

After all, group_by was adapted by Ruby 1.9 after a lot of field 
experience in Rails. And I have personally used it many times, as I 
think others have, too.

On the other hand, do you have that much field experience? How many 
others have told you that they would have used categorize a few times 
already if it existed? (As opposed to those who have said that it's too 
complicated to remember what it does, and too easy to write the 
equivalent by hand if needed.)

An additional thought: The example above starts with two-element arrays. 
Such two- or multi-element arrays are often used, but in many cases they 
are just an intermediate step, before creating objects. group_by seems 
more close to using objects (that may be why it is used a lot in Rails, 
where the basics of model classes are almost free). On the other hand, 
with multi-element arrays, I think that part of what "categorize" would 
do will often be handled before or after. Anyway, while we should not 
change Ruby so that it is too difficult to use multi-element arrays 
instead of objects, there is also no reason to create more methods that 
work better for multi-element arrays.

>> Who will be able to remember exactly what these options are,...? Most other
>> methods, in particular on Enumerables, don't have that many options. I think
>> that's for a good reason.
>
> I think option argument is adopted to ruby increasingly.

That may be true. But even if the average number of options for a Ruby 
method has slightly increased recently, your proposals still is way over 
average on the number of options, especially in an area (iterators on 
Enumerable) where options are few and far between.

Regards,   Martin.

-- 
#-# Martin J. Dürst, Professor, Aoyama Gakuin University
#-# http://www.sw.it.aoyama.ac.jp   mailto:duerst / it.aoyama.ac.jp