[Issue 11097] New: Add version of std.algorithm.group that returns group ranges

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Sep 22 05:58:51 PDT 2013


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

           Summary: Add version of std.algorithm.group that returns group
                    ranges
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: peter.alexander.au at gmail.com


--- Comment #0 from Peter Alexander <peter.alexander.au at gmail.com> 2013-09-22 05:58:50 PDT ---
std.algorithm.group returns a range of (elem, count) tuples:

int[] arr = [ 1, 2, 2, 2, 2, 3, 4, 4, 4, 5 ];
assert(equal(group(arr), [ tuple(1, 1u), tuple(2, 4u), tuple(3, 1u),
    tuple(4, 3u), tuple(5, 1u) ][]));

This is fine when the predicate is equality, but when the predicate is
something else, the tuple is less useful. Here's an example of grouping strings
by first character:

string[] arr = [ "Alice", "Andrew", "Ben", "Bob" ];
assert(equal(group!("a.front == b.front")(arr),
    [ tuple("Alice", 2), tuple("Ben", 2) ]));

This isn't very useful because there aren't two Alice's and two Ben's. Alice
and Ben are just one element from the group (btw, the documentation doesn't
indicate that it is always the first element in the group that is returned).

It would be nice if there was a version of the algorithm that returned the
groups themselves, working like this:

string[] arr = [ "Alice", "Andrew", "Ben", "Bob" ];
assert(equal(groups!("a.front == b.front")(arr),
    [ ["Alice", "Andrew"], ["Ben", "Bob"] ]));

I have used the identifier "groups" here. I'm not bothered what it is called.

Once implemented, group may be elegantly implemented in terms of groups:

auto group(alias f, R)(R r)
{
    return groups!(f)(r).map!(g => tuple(g.front, g.walkLength));
}

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