[Issue 13595] Extend std.algorithm.groupBy to support non-equivalence relations

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Dec 1 02:50:50 PST 2014


https://issues.dlang.org/show_bug.cgi?id=13595

--- Comment #7 from bearophile_hugs at eml.cc ---
As second try I've used groupBy to find the longest anagrams in a dictionary of
words. "words.txt" is a text file that contains one different word each line.


void main() {
    import std.stdio, std.algorithm, std.string, std.file, std.array;

    auto anags =
         "words.txt"
         .readText
         .split
         .schwartzSort!((string w) => w.dup.representation.sort().release)
         .groupBy!((w1, w2) =>
w1.dup.representation.sort().equal(w2.dup.representation.sort()))
         .map!array
         .array
         .sort!((g1, g2) => g1.length > g2.length)
         .release
         .groupBy!((g1, g2) => g1.length == g2.length)
         .front;
    writefln("%(%s\n%)", anags);
}



If I replace "(string w)" with "w" in the first schwartzSort I receive the
errors:

...\dmd2\src\phobos\std\algorithm.d(4659,5): Error: field _prev must be
initialized in constructor, because it is nested struct
...\dmd2\src\phobos\std\algorithm.d(4770,51): Error: template instance
anagrams1c.main.groupBy!(__lambda2, cast(Flag)false, SortedRange!(string[], (a,
b) => binaryFun!less(unaryFun!transform(a), unaryFun!transform(b)))) error
instantiating
anagrams1c.d(9,10):        instantiated from here: groupBy!((w1, w2) =>
w1.dup.representation.sort().equal(w2.dup.representation.sort()),
SortedRange!(string[], (a, b) => binaryFun!less(unaryFun!transform(a),
unaryFun!transform(b))))


If I remove the last ".release" (at line 13) I receive the similar errors:

...\dmd2\src\phobos\std\algorithm.d(4659,5): Error: field _prev must be
initialized in constructor, because it is nested struct
...\dmd2\src\phobos\std\algorithm.d(4770,51): Error: template instance
anagrams1c.main.groupBy!(__lambda4, cast(Flag)false, SortedRange!(string[][],
__lambda3)) error instantiating
anagrams1c.d(14,10):        instantiated from here: groupBy!((g1, g2) =>
g1.length == g2.length, SortedRange!(string[][], __lambda3))


Is this a problem of groupBy?

--


More information about the Digitalmars-d-bugs mailing list