Bug 19220 – multiSort and SortedRange.groupBy do not play together
Status
RESOLVED
Resolution
WORKSFORME
Severity
major
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
All
Creation time
2018-09-04T21:32:25Z
Last change time
2020-04-27T14:37:50Z
Assigned to
No Owner
Creator
Justin
Comments
Comment #0 by mrjnewt — 2018-09-04T21:32:25Z
std.algorithm.multiSort returns a SortedRange which in turn has a groupBy method. Usage of this method does not actually compile when sorting by more than one predicate. Example repro (online here: https://run.dlang.io/is/mQGAvD):
import std.stdio;
import std.algorithm;
void main()
{
auto records = [
Record(10, 10),
Record(12, 1),
Record(30, 12),
Record(10, 10),
];
{
// Works:
auto groups = records.multiSort!(`a.x < b.x`).groupBy;
writeln(groups);
}
{
// Doesn't work:
auto groups = records.multiSort!(`a.x < b.x`, `a.y < b.y`).groupBy;
writeln(groups);
}
}
struct Record
{
int x;
int y;
}
SortedRange.groupBy apparently delegates the work to chunkBy which chokes on the predicate defined by multiSort:
/dlang/dmd-beta/linux/bin64/../../src/phobos/std/algorithm/iteration.d(1821): Error: static assert: "chunkBy expects either a binary predicate or a unary predicate on range elements of type: Record"
/dlang/dmd-beta/linux/bin64/../../src/phobos/std/algorithm/iteration.d(1896): instantiated from here: ChunkByImplIsUnary!(__lambda1, Record[])
/dlang/dmd-beta/linux/bin64/../../src/phobos/std/algorithm/iteration.d(2110): instantiated from here: ChunkByImpl!(__lambda1, Record[])
/dlang/dmd-beta/linux/bin64/../../src/phobos/std/range/package.d(10730): instantiated from here: chunkBy!((a, b) => !predFun(a, b) && !predFun(b, a), Record[])
onlineapp.d(21): instantiated from here: groupBy!()
Commenting out the "doesn't work" section demonstrates that multiSort's predicate works fine as long there's a single original predicate.
Comment #1 by moonlightsentinel — 2020-04-27T14:37:50Z