[Issue 5502] More handy ways to create associative arrays

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri May 24 11:22:07 PDT 2013


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



--- Comment #3 from bearophile_hugs at eml.cc 2013-05-24 11:22:04 PDT ---
Regarding the second suggestion, now I prefer a more general solution.

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

As use case this program shows some of the anagrams, given a file of different
lowercase words:


import std.stdio, std.algorithm, std.range, std.string, std.file;

void main() {
    string[][const ubyte[]] anags;
    foreach (string w; std.array.splitter("unixdict.txt".readText))
        anags[w.dup.representation.sort().release.idup] ~= w;

    writefln("%-(%s\n%)", anags
                          .byValue
                          .filter!q{ a.length > 2 }
                          .take(20));
}



A gather() higher order function allows to remove the foreach and replace it
with:


const anags = std.array.splitter("unixdict.txt".readText)
              .gather(w => w.dup.representation.sort().release.idup);


gather() takes a callable that specifies how to compute the key of the
associative array.

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