mirror of
https://github.com/dlang/phobos.git
synced 2025-04-30 15:10:46 +03:00
make canFind predicate-able
This commit is contained in:
parent
f5e45c7626
commit
a84a69dc8c
1 changed files with 32 additions and 24 deletions
|
@ -10587,11 +10587,25 @@ unittest
|
|||
+/
|
||||
|
||||
// canFind
|
||||
/**
|
||||
/++
|
||||
Convenience function. Like find, but only returns whether or not the search
|
||||
was succesful.
|
||||
+/
|
||||
template canFind(alias pred="a == b")
|
||||
{
|
||||
//Explictly Undocumented. Do not use. It may be deprecated in the future.
|
||||
//Use any instead.
|
||||
bool canFind(Range)(Range haystack)
|
||||
if (is(typeof(find!pred(haystack))))
|
||||
{
|
||||
return any!pred(haystack);
|
||||
}
|
||||
|
||||
/++
|
||||
Returns $(D true) if and only if $(D value) can be found in $(D
|
||||
range). Performs $(BIGOH needle.length) evaluations of $(D pred).
|
||||
*/
|
||||
bool canFind(alias pred = "a == b", R, E)(R haystack, E needle)
|
||||
+/
|
||||
bool canFind(Range, Element)(Range haystack, Element needle)
|
||||
if (is(typeof(find!pred(haystack, needle))))
|
||||
{
|
||||
return !find!pred(haystack, needle).empty;
|
||||
|
@ -10608,13 +10622,14 @@ if (is(typeof(find!pred(haystack, needle))))
|
|||
without having to deal with the tuple that $(D LREF find) returns for the
|
||||
same operation.
|
||||
+/
|
||||
size_t canFind(alias pred = "a == b", Range, Ranges...)(Range haystack, Ranges needles)
|
||||
size_t canFind(Range, Ranges...)(Range haystack, Ranges needles)
|
||||
if (Ranges.length > 1 &&
|
||||
allSatisfy!(isForwardRange, Ranges) &&
|
||||
is(typeof(find!pred(haystack, needles))))
|
||||
{
|
||||
return find!pred(haystack, needles)[1];
|
||||
}
|
||||
}
|
||||
|
||||
unittest
|
||||
{
|
||||
|
@ -10638,13 +10653,6 @@ unittest
|
|||
assert(canFind([0, 1, 2, 3], [1, 3], [2, 4]) == 0);
|
||||
}
|
||||
|
||||
//Explictly Undocumented. Do not use. It may be deprecated in the future.
|
||||
//Use any instead.
|
||||
bool canFind(alias pred, Range)(Range range)
|
||||
{
|
||||
return any!pred(range);
|
||||
}
|
||||
|
||||
/**
|
||||
Returns $(D true) if and only if a value $(D v) satisfying the
|
||||
predicate $(D pred) can be found in the forward range $(D
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue