Function difference
Creates a range of values not included in the other given ranges.
auto auto difference(alias pred, Range, Rs...)
(
Range range,
Rs values
)
if (isRangeOverValidPredicate!(pred, Range));
You may pass in a unary or binary predicate. If a unary predicate is passed in, then a transformation will be appled to each element before comparing. If a binary predicate is passed in, it must be a comparator predicate that returns true if if a < b.
If pred
is null or unary, and the range is sortable, a sort is applied followed by an
optimized linear algorithm. If the range is already sorted and pred
is null, then the linear algorithm
is just used with the sorted rang's sorting predicate
.
Parameters
Name | Description |
---|---|
pred | unary transformation or binary comparator |
range | the range to inspect |
values | ranges or single values to exclude |
Returns
New array of filtered results. If Rs
is empty, then range
is returned
Since
0.0.1
Example
assert([1, 2, 3] .difference([0, 1, 2]) .equal([3]));
assert([1, 2, 3] .difference([1, 2]) .equal([3]));
assert([1, 2, 3] .difference([1], 2) .equal([3]));
assert([1, 2, 3] .difference([1], [3]) .equal([2]));
assert([1, 2, 3] .difference(3) .equal([1, 2]));
Example
// Implicitly convertible elements ok
assert([1.0, 2.0] .difference(2) .equal([1.0]));
// Implicitly convertible ranges ok
assert([1.0, 2.0] .difference([2]) .equal([1.0]));
// Non implicily convertible elements not ok
static assert(!__traits(compiles, [1] .difference(1.0)));
// Non implicily convertible range not ok
static assert(!__traits(compiles, [1] .difference([1.0])));
Example
import std .math: ceil;
assert([2.1, 1.2] .difference!ceil([2.3, 3.4]) .equal([1.2]));
assert([9, 2, 3, 2, 3, 9] .difference([7, 3, 1, 5]) .equal([2, 2, 9, 9]));