www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9842] New: std.algorithm.hashGroup / hashGroupBy

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 puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs 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: -------
Mar 30 2013