The OpenD Programming Language

uniq

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.

  1. Uniq!(naryFun!pred, Range) uniq(Range r)
    template uniq(alias pred = "a == b")
    static if(__traits(isSame, naryFun!pred, pred))
    Uniq!(naryFun!pred, Range)
    uniq
    (
    Range
    )
    (
    Range r
    )
    if (
    isInputRange!Range &&
    !isSlice!Range
    )
  2. auto uniq(Slice!(Iterator, N, kind) slice)

Members

Functions

uniq
Uniq!(naryFun!pred, Range) uniq(Range r)
auto uniq(Slice!(Iterator, N, kind) slice)

Parameters

pred

Predicate for determining equivalence between range elements.

Examples

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] ]));

Meta