[Issue 5502] More handy ways to create associative arrays

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat May 25 04:03:33 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=5502



--- Comment #4 from bearophile_hugs at eml.cc 2013-05-25 04:03:30 PDT ---
(In reply to comment #3)

> One possible name for this free function that returns an associative array is
> "gather", as maybe in Perl6.

I was wrong, in Perl6 it's named "classify" (List.classify):
http://doc.perl6.org/routine/classify


multi sub    classify(&mapper, *@values) returns Hash:D
multi method classify(List:D: &mapper)   returns Hash:D

Transforms a list of values into a hash representing the classification of
those values according to a mapper; each hash key represents the classification
for one or more of the incoming list values, and the corresponding hash value
contains an array of those list values classified by the mapper into the
category of the associated key.

Examples:

say classify { $_ %% 2 ?? 'even' !! 'odd' }, (1, 7, 6, 3, 2);
# ("odd" => [1, 7, 3], "even" => [6, 2]).hash;;

say ('hello', 1, 22/7, 42, 'world').classify: { .Str.chars }
# ("5" => ["hello", "world"], "1" => [1], "8" => [22/7], "2" => [42]).hash

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list