chunkBy

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

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 binaryFun, or unary, which is passed to 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. Use splitWhen if you want to chunk by a predicate that is not an equivalence relation.

Parameters

predPredicate for determining equivalence.
rAn input range to be chunked.

Returns

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. Copying the range currently has reference semantics, but this may change in the future.

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.

See_also:

group, which collapses adjacent equivalent elements into a single

element.