dranges.predicate

This module contains some usual predicates, either on entire ranges or on their elements.

License:
Boost License 1.0.

Authors:
Philippe Sigaud

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

bool some (alias pred, R)(R range);
Is true if some (at least one) element in range verify the predicate pred. False otherwise. Typically, it's used with a unary (1-arg) predicate, but it accepts predicates of any arity. If the predicate is a n-args function (n>1), it will be presented with n-elements 'segments' of the range, with a step of one: interleaved segments.

See Also:
dranges.range.segment.

TODO:
add an optional step argument?

Example:
int[] r1 = [0,1,2,3];
int[] r2 = [];
assert(some!"a>0"(r1));
assert(!some!"a>3"(r1));
assert(!some!"a>0"(r2)); // empty range: no element can verify the predicate

assert(some!"a<b"(r1));  // Increasing range
assert(!some!"b<a"(r1));
assert(!some!"a<f"(r1)); // 6-args function -> no match -> some is false

assert(some!"true"(r1)); // degenerate case: nullary predicate.
assert(!some!"false"(r1));


bool all (alias pred, R)(R range);
Is true if all elements in range verify the predicate pred. False otherwise. Typically, it's used with a unary (1-arg) predicate, but it accepts predicates of any arity. If the predicate is a n-args function (n>1), it will be presented with n-elements 'segments' of the range, with a step of one: interleaved segments.

See Also:
dranges.range.segment.

TODO:
add an optional step argument?
int[] r1 = [0,1,2,3];
int[] r2 = [];
assert(all!"a>=0"(r1));
assert(!all!"a>0"(r1));
assert(all!"a>0"(r2)); // empty range: any property is verified by the (inexistent) elements

assert(all!"a<b"(r1));  // Binary function. Tests for an increasing range
assert(!all!"b<a"(r1));
assert(all!"a<f"(r1)); // 6-args function -> no match -> empty segment range -> all is true

assert(all!"true"(r1)); // degenerate case: nullary predicate.
assert(!all!"false"(r1));


bool isStrictlyIncreasing (R)(R range);
bool isIncreasing (R)(R range);
bool isStrictlyDecreasing (R)(R range);
bool isDecreasing (R)(R range);
bool isConstant (R)(R range);
bool hasNoPair (R)(R range);
A bunch of small one-liners, using all. Their names may be subject to change, as I don't like them much. The tests are (respectively): "ab","a>=b", "a==b" and "a!=b".

bool allDifferent (R)(R range);
Return:
true iff all the range elements are different (as tested with inclusion in an associative array).

TODO:
Maybe another version with an aliased predicate? I don't know how to use an AA in this case, so that would be much slower. But that would allow using approxEqual on floating point elements.

bool someEmpty (R...)(R ranges);
Is true iff some ranges in the variadic list ranges are empty. False otherwise.

bool allEmpty (R...)(R ranges);
If true iff all ranges are empty.

bool allVerify (alias pred, R...)(R ranges);
Given a variadic list of ranges, returns true iff all ranges in ranges verify pred. The difference with std.traits.allSatisfy is that the latter act on types, whereas allVerify act on instances (values, whatever). The difference with 'all' is that 'all' acts on one range, and tests the predicate on the range's elements.

Example:
int[] r1 = [0,1,2];
string r2 = "abc";
assert(allVerify!"a.length > 2"(r1,r2));


bool someVerify (alias pred, R...)(R ranges);
Given a variadic list of ranges, returns true iff some (at least one) ranges in ranges verify the predicate pred.

Example:
int[] r1 = [0,1,2];
string r2 = "abcde";
assert(someVerify!"a.length > 4"(r2));


bool isOneOf (alias range)(Unqual!(ElementType!(typeof(range))) element);
Returns true iff element is one of the elements of range. Used as a predicate to find a subrange inside another range: dropWhile!( isOneOf !"abc")(r1) It may never terminate if range is infinite, but as an optimization, isOneOf detects Cycle!U and works only on the cycle internal range.

See Also:
dranges.algorithm.contains. Maybe I could define one with another...

Example:
string s = "01212345";
auto m = map!(isOneOf!"012")(s);
assert(equal(m, [true,true,true,true,true,false,false,false][]));

auto c = cycle("012");
auto m2 = map!(isOneOf!c)(s); // doesn't hang, as it's equivalent to isOneOf!"012"
assert(equal(m2, m));


bool allEqual (T...)(T vals);
Returns true iff if all values in the variadic list vals are equal.

Examples:
assert(allEqual(1,1.0,1.0000,1)); // The values are automatically converted
assert(allEqual(1)); // one element, always true.
assert(allEqual()); // no element, always true.
assert(!allEqual(1,2));


bool equals (alias value, T)(T t);
Returns true if t equals value. If value and t don't have compatible types, it returns false.

Example:
int[] r = [0,1,0,1,2,0,3];
assert(equals!0(r.front));

auto m = map!(equals!(0,int))(r);
assert(equal(m, [true,false,true,false,false,true,false][]));

assert(!equals!"a"(r.front));


bool delegate(T) equals (T)(T t);
curried form of equals .

bool isOdd (T)(T t);
bool isEven (T)(T t);
Small simple predicates on integral types.

template Not (alias fun)
Takes a predicate and inverts it. It's an attempt to get std.functional.not to work. I've had some difficulties to use it in filters.

See Also:
dranges.algorithm.separate.

Example:
int[] r = [0,1,2,3,4,5];
auto f1 = filter!isOdd(r);
auto f2 = filter!(Not!isEven)(r);
assert(equal(f1,f2));


struct Nub (T);
Nub!(T) nub (T)();


Page was generated with on Fri Nov 12 11:55:11 2010