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).

Members

Functions

empty
bool empty()
Undocumented in source. Be warned that the author may not have intended to support it.
front
auto ref front()
Undocumented in source. Be warned that the author may not have intended to support it.
opSlice
ref opSlice()
Undocumented in source. Be warned that the author may not have intended to support it.
popFront
void popFront()
Undocumented in source. Be warned that the author may not have intended to support it.

Properties

save
typeof(this) save [@property getter]
Undocumented in source. Be warned that the author may not have intended to support it.

Variables

_freshEmpty
bool _freshEmpty;
Undocumented in source.
_input
Range _input;
Undocumented in source.

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