filter

Implements the higher order filter function. The predicate is passed to mir.functional.naryFun, and can either accept a string, or any callable that can be executed via pred(element).

  1. Filter!(naryFun!pred, Range) filter(Range r)
    template filter(alias pred = "a")
    @optmath
    static if(__traits(isSame, naryFun!pred, pred))
    Filter!(naryFun!pred, Range)
    filter
    (
    Range
    )
    (
    Range r
    )
    if (
    isInputRange!Range &&
    !isSlice!Range
    )
  2. auto filter(Slice!(Iterator, N, kind) slice)
  3. alias filter = .filter!(naryFun!pred)

Members

Aliases

filter
alias filter = .filter!(naryFun!pred)
Undocumented in source.

Functions

filter
auto filter(Slice!(Iterator, N, kind) slice)
filter
Filter!(naryFun!pred, Range) filter(Range r)

Parameters

pred

Function to apply to each element of range

Return Value

filter!(pred)(range) returns a new range containing only elements x in range for which pred(x) returns true.

Examples

int[] arr = [ 0, 1, 2, 3, 4, 5 ];

// Filter below 3
auto small = filter!(a => a < 3)(arr);
assert(equal(small, [ 0, 1, 2 ]));

// Filter again, but with Uniform Function Call Syntax (UFCS)
auto sum = arr.filter!(a => a < 3);
assert(equal(sum, [ 0, 1, 2 ]));

// Filter with the default predicate
auto nonZeros = arr.filter;
assert(equal(nonZeros, [ 1, 2, 3, 4, 5 ]));

// In combination with concatenation() to span multiple ranges
import mir.ndslice.concatenation;

int[] a = [ 3, -2, 400 ];
int[] b = [ 100, -101, 102 ];
auto r = concatenation(a, b).filter!(a => a > 0);
assert(equal(r, [ 3, 400, 100, 102 ]));

// Mixing convertible types is fair game, too
double[] c = [ 2.5, 3.0 ];
auto r1 = concatenation(c, a, b).filter!(a => cast(int) a != a);
assert(equal(r1, [ 2.5 ]));

N-dimensional filtering

import mir.ndslice.fuse;
import mir.ndslice.topology: byDim, map;

auto matrix =
    [[   3,   -2, 400 ],
     [ 100, -101, 102 ]].fuse;

alias filterPositive = filter!"a > 0";

// filter all elements in the matrix
auto r = filterPositive(matrix);
assert(equal(r, [ 3, 400, 100, 102 ]));

// filter all elements for each row
auto rr = matrix.byDim!0.map!filterPositive;
assert(equal!equal(rr, [ [3, 400], [100, 102] ]));

// filter all elements for each column
auto rc = matrix.byDim!1.map!filterPositive;
assert(equal!equal(rc, [ [3, 100], [], [400, 102] ]));

See Also

Filter (higher-order function) Note: User and library code MUST call empty method ahead each call of pair or one of front and popFront methods.

Meta