www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16588] New: uniq's BidirectionalRange behavior is

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

          Issue ID: 16588
           Summary: uniq's BidirectionalRange behavior is inconsistent
                    with its InputRange behavior
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: acehreli yahoo.com

import std.algorithm;

struct S {
    int i;
    string s;
}

void main() {
    auto arr = [ S(1, "a"), S(1, "b"),
                 S(2, "c"), S(2, "d")];

    // Let's consider just the 'i' member for equality
    auto r = arr.uniq!((a, b) => a.i == b.i);

    assert(r.equal([S(1, "a"), S(2, "c")]));    // makes sense

    // Since there are just 2 elements, we expect the following
    assert(r.front == S(1, "a"));               // good, consistent
    assert(r.back == S(2, "c"));                // FAILS

    // Instead, the following passes but it's unexpected
    assert(r.back == S(2, "d"));
}

Bonus: uniq could take a PickStrategy template parameter so that the caller
could specify which of the unique elements of each sequence to pick, either the
first or the last.

Ali

--
Oct 03 2016