[Issue 9842] New: std.algorithm.hashGroup / hashGroupBy

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Mar 30 14:32:39 PDT 2013


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

           Summary: std.algorithm.hashGroup / hashGroupBy
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2013-03-30 14:32:38 PDT ---
A little Strems/LINQ challenge, "Grouping by a Criterium":

Group the elements of a collection of strings by their length.

A C# LINQ solution:

string[] names = {"Sam", "Samuel", "Samu", "Ravi", "Ratna",  "Barsha"};
var groups = names.GroupBy(c => c.Length);


Using the groupBy written by Andrei
(https://github.com/D-Programming-Language/phobos/pull/1186 ) a D solution is:

    auto names = ["Sam", "Samuel", "Samu", "Ravi", "Ratna", "Barsha"];
    auto groups = names
                  .schwartzSort!q{ a.length }
                  .groupBy!q{ a.length == b.length };


But group/groupBy work by sorting. Hash-based O(n) hashGroup/hashGroupBy are
also conceivable, potentially faster, and leading to simpler code, because you
don't need to sort the items first:

    auto names = ["Sam", "Samuel", "Samu", "Ravi", "Ratna", "Barsha"];
    auto groups = names.hashGroupBy!q{ a.length == b.length };


(Alternative names for hashGroup/hashGroupBy are possible.)

-- 
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