Predicate for determining equivalence between range elements.
int[] arr = [ 1, 2, 2, 2, 2, 3, 4, 4, 4, 5 ]; assert(equal(uniq(arr), [ 1, 2, 3, 4, 5 ])); import std.algorithm.mutation : copy; // Filter duplicates in-place using copy arr.length -= arr.uniq.copy(arr).length; assert(arr == [ 1, 2, 3, 4, 5 ]); // Note that uniqueness is only determined consecutively; duplicated // elements separated by an intervening different element will not be // eliminated: assert(equal(uniq([ 1, 1, 2, 1, 1, 3, 1]), [1, 2, 1, 3, 1]));
N-dimensional case
import mir.ndslice.fuse; import mir.ndslice.topology: byDim, map, iota; auto matrix = [ [1, 2, 2], [2, 2, 3], [4, 4, 4] ].fuse; assert(matrix.uniq.equal([ 1, 2, 3, 4 ])); // unique elements for each row assert(matrix.byDim!0.map!uniq.equal!equal([ [1, 2], [2, 3], [4] ]));
Lazily iterates unique consecutive elements of the given range (functionality akin to the _uniq system utility). Equivalence of elements is assessed by using the predicate pred, by default "a == b". The predicate is passed to mir.functional.nary, and can either accept a string, or any callable that can be executed via pred(element, element). If the given range is bidirectional, uniq also yields a std,range,primitives.