mirror of
https://github.com/dlang/phobos.git
synced 2025-04-30 07:00:37 +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,17 +10587,31 @@ unittest
|
||||||
+/
|
+/
|
||||||
|
|
||||||
// canFind
|
// canFind
|
||||||
/**
|
|
||||||
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)
|
|
||||||
if (is(typeof(find!pred(haystack, needle))))
|
|
||||||
{
|
|
||||||
return !find!pred(haystack, needle).empty;
|
|
||||||
}
|
|
||||||
|
|
||||||
/++
|
/++
|
||||||
|
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(Range, Element)(Range haystack, Element needle)
|
||||||
|
if (is(typeof(find!pred(haystack, needle))))
|
||||||
|
{
|
||||||
|
return !find!pred(haystack, needle).empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
/++
|
||||||
Returns the 1-based index of the first needle found in $(D haystack). If no
|
Returns the 1-based index of the first needle found in $(D haystack). If no
|
||||||
needle is found, then $(D 0) is returned.
|
needle is found, then $(D 0) is returned.
|
||||||
|
|
||||||
|
@ -10607,13 +10621,14 @@ if (is(typeof(find!pred(haystack, needle))))
|
||||||
$(D bool) for the same effect or used to get which needle was found first
|
$(D bool) for the same effect or used to get which needle was found first
|
||||||
without having to deal with the tuple that $(D LREF find) returns for the
|
without having to deal with the tuple that $(D LREF find) returns for the
|
||||||
same operation.
|
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 &&
|
if (Ranges.length > 1 &&
|
||||||
allSatisfy!(isForwardRange, Ranges) &&
|
allSatisfy!(isForwardRange, Ranges) &&
|
||||||
is(typeof(find!pred(haystack, needles))))
|
is(typeof(find!pred(haystack, needles))))
|
||||||
{
|
{
|
||||||
return find!pred(haystack, needles)[1];
|
return find!pred(haystack, needles)[1];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unittest
|
unittest
|
||||||
|
@ -10638,13 +10653,6 @@ unittest
|
||||||
assert(canFind([0, 1, 2, 3], [1, 3], [2, 4]) == 0);
|
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
|
Returns $(D true) if and only if a value $(D v) satisfying the
|
||||||
predicate $(D pred) can be found in the forward range $(D
|
predicate $(D pred) can be found in the forward range $(D
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue