filterBidirectional

Similar to filter, except it defines a bidirectional range. There is a speed disadvantage - the constructor spends time finding the last element in the range that satisfies the filtering condition (in addition to finding the first one). The advantage is that the filtered range can be spanned from both directions. Also, std.range.retro can be applied against the filtered range.

The predicate is passed to std.functional.unaryFun, and can either accept a string, or any callable that can be executed via pred(element).

template filterBidirectional(alias pred)
filterBidirectional
(
Range
)
(
Range r
)
if (
isBidirectionalRange!(Unqual!Range)
)

Members

Functions

filterBidirectional
auto filterBidirectional(Range r)

Parameters

pred

Function to apply to each element of range

Examples

import std.algorithm.comparison : equal;
import std.range;

int[] arr = [ 1, 2, 3, 4, 5 ];
auto small = filterBidirectional!("a < 3")(arr);
static assert(isBidirectionalRange!(typeof(small)));
assert(small.back == 2);
assert(equal(small, [ 1, 2 ]));
assert(equal(retro(small), [ 2, 1 ]));
// In combination with chain() to span multiple ranges
int[] a = [ 3, -2, 400 ];
int[] b = [ 100, -101, 102 ];
auto r = filterBidirectional!("a > 0")(chain(a, b));
assert(r.back == 102);

Meta