chunkBy

Chunks an input range into subranges of equivalent adjacent elements. In other languages this is often called partitionBy, groupBy or sliceWhen.

Equivalence is defined by the predicate pred, which can be either binary, which is passed to std.functional.binaryFun, or unary, which is passed to std.functional.unaryFun. In the binary form, two _range elements a and b are considered equivalent if pred(a,b) is true. In unary form, two elements are considered equivalent if pred(a) == pred(b) is true.

This predicate must be an equivalence relation, that is, it must be reflexive (pred(x,x) is always true), symmetric (pred(x,y) == pred(y,x)), and transitive (pred(x,y) && pred(y,z) implies pred(x,z)). If this is not the case, the range returned by chunkBy may assert at runtime or behave erratically.

chunkBy
(
alias pred
Range
)
(
Range r
)
if (
isInputRange!Range
)

Parameters

pred

Predicate for determining equivalence.

r
Type: Range

An input range to be chunked.

Return Value

Type: auto

With a binary predicate, a range of ranges is returned in which all elements in a given subrange are equivalent under the given predicate. With a unary predicate, a range of tuples is returned, with the tuple consisting of the result of the unary predicate for each subrange, and the subrange itself.

Notes:

Equivalent elements separated by an intervening non-equivalent element will appear in separate subranges; this function only considers adjacent equivalence. Elements in the subranges will always appear in the same order they appear in the original range.

Examples

Showing usage with binary predicate:

1 import std.algorithm.comparison : equal;
2 
3 // Grouping by particular attribute of each element:
4 auto data = [
5     [1, 1],
6     [1, 2],
7     [2, 2],
8     [2, 3]
9 ];
10 
11 auto r1 = data.chunkBy!((a,b) => a[0] == b[0]);
12 assert(r1.equal!equal([
13     [[1, 1], [1, 2]],
14     [[2, 2], [2, 3]]
15 ]));
16 
17 auto r2 = data.chunkBy!((a,b) => a[1] == b[1]);
18 assert(r2.equal!equal([
19     [[1, 1]],
20     [[1, 2], [2, 2]],
21     [[2, 3]]
22 ]));

Showing usage with unary predicate:

1 import std.algorithm.comparison : equal;
2 import std.range.primitives;
3 import std.typecons : tuple;
4 
5 // Grouping by particular attribute of each element:
6 auto range =
7 [
8     [1, 1],
9     [1, 1],
10     [1, 2],
11     [2, 2],
12     [2, 3],
13     [2, 3],
14     [3, 3]
15 ];
16 
17 auto byX = chunkBy!(a => a[0])(range);
18 auto expected1 =
19 [
20     tuple(1, [[1, 1], [1, 1], [1, 2]]),
21     tuple(2, [[2, 2], [2, 3], [2, 3]]),
22     tuple(3, [[3, 3]])
23 ];
24 foreach (e; byX)
25 {
26     assert(!expected1.empty);
27     assert(e[0] == expected1.front[0]);
28     assert(e[1].equal(expected1.front[1]));
29     expected1.popFront();
30 }
31 
32 auto byY = chunkBy!(a => a[1])(range);
33 auto expected2 =
34 [
35     tuple(1, [[1, 1], [1, 1]]),
36     tuple(2, [[1, 2], [2, 2]]),
37     tuple(3, [[2, 3], [2, 3], [3, 3]])
38 ];
39 foreach (e; byY)
40 {
41     assert(!expected2.empty);
42     assert(e[0] == expected2.front[0]);
43     assert(e[1].equal(expected2.front[1]));
44     expected2.popFront();
45 }

See Also

group, which collapses adjacent equivalent elements into a single element.

Meta