www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19220] New: multiSort and SortedRange.groupBy do not play


          Issue ID: 19220
           Summary: multiSort and SortedRange.groupBy do not play together
           Product: D
           Version: D2
          Hardware: x86_64
                OS: All
            Status: NEW
          Severity: major
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: mrjnewt gmail.com

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:

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;

        // Doesn't work:
        auto groups = records.multiSort!(`a.x < b.x`, `a.y < b.y`).groupBy;

struct Record
    int x;
    int y;

SortedRange.groupBy apparently delegates the work to chunkBy which chokes on
the predicate defined by multiSort:

Error: static assert:  "chunkBy expects either a binary predicate or a unary
predicate on range elements of type: Record"
     instantiated from here: ChunkByImplIsUnary!(__lambda1, Record[])
     instantiated from here: ChunkByImpl!(__lambda1, Record[])
instantiated from here: chunkBy!((a, b) => !predFun(a, b) && !predFun(b, a),
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.

Sep 04 2018