mirror of
https://github.com/dlang/phobos.git
synced 2025-04-29 14:40:30 +03:00
Simplify the example for find
This commit is contained in:
parent
2ff0a05ca0
commit
a6688108a1
1 changed files with 21 additions and 27 deletions
|
@ -1473,6 +1473,9 @@ Elements of `haystack` are compared with `needle` by using predicate
|
||||||
`pred` with `pred(haystack.front, needle)`.
|
`pred` with `pred(haystack.front, needle)`.
|
||||||
`find` performs $(BIGOH walkLength(haystack)) evaluations of `pred`.
|
`find` performs $(BIGOH walkLength(haystack)) evaluations of `pred`.
|
||||||
|
|
||||||
|
The predicate is passed to $(REF binaryFun, std, functional), and can either accept a
|
||||||
|
string, or any callable that can be executed via `pred(element, element)`.
|
||||||
|
|
||||||
To _find the last occurrence of `needle` in a
|
To _find the last occurrence of `needle` in a
|
||||||
$(REF_ALTTEXT bidirectional, isBidirectionalRange, std,range,primitives) `haystack`,
|
$(REF_ALTTEXT bidirectional, isBidirectionalRange, std,range,primitives) `haystack`,
|
||||||
call `find(retro(haystack), needle)`. See $(REF retro, std,range).
|
call `find(retro(haystack), needle)`. See $(REF retro, std,range).
|
||||||
|
@ -1664,38 +1667,29 @@ if (isInputRange!InputRange &&
|
||||||
///
|
///
|
||||||
@safe unittest
|
@safe unittest
|
||||||
{
|
{
|
||||||
import std.algorithm.comparison : equal;
|
import std.range.primitives;
|
||||||
import std.container : SList;
|
|
||||||
import std.range;
|
|
||||||
import std.range.primitives : empty;
|
|
||||||
|
|
||||||
auto arr = assumeSorted!"a < b"([1, 2, 4, 4, 4, 4, 5, 6, 9]);
|
auto arr = [1, 2, 4, 4, 4, 4, 5, 6, 9];
|
||||||
assert(find(arr, 4) == assumeSorted!"a < b"([4, 4, 4, 4, 5, 6, 9]));
|
assert(arr.find(4) == [4, 4, 4, 4, 5, 6, 9]);
|
||||||
assert(find(arr, 1) == arr);
|
assert(arr.find(1) == arr);
|
||||||
assert(find(arr, 9) == assumeSorted!"a < b"([9]));
|
assert(arr.find(9) == [9]);
|
||||||
assert(find!"a > b"(arr, 4) == assumeSorted!"a < b"([5, 6, 9]));
|
assert(arr.find!((a, b) => a > b)(4) == [5, 6, 9]);
|
||||||
assert(find!"a < b"(arr, 4) == arr);
|
assert(arr.find!((a, b) => a < b)(4) == arr);
|
||||||
assert(find(arr, 0).empty());
|
assert(arr.find(0).empty);
|
||||||
assert(find(arr, 10).empty());
|
assert(arr.find(10).empty);
|
||||||
assert(find(arr, 8).empty());
|
assert(arr.find(8).empty);
|
||||||
|
|
||||||
auto r = assumeSorted!"a > b"([10, 7, 3, 1, 0, 0]);
|
|
||||||
assert(find(r, 3) == assumeSorted!"a > b"([3, 1, 0, 0]));
|
|
||||||
assert(find!"a > b"(r, 8) == r);
|
|
||||||
assert(find!"a < b"(r, 5) == assumeSorted!"a > b"([3, 1, 0, 0]));
|
|
||||||
|
|
||||||
assert(find("hello, world", ',') == ", world");
|
assert(find("hello, world", ',') == ", world");
|
||||||
assert(find([1, 2, 3, 5], 4) == []);
|
}
|
||||||
assert(equal(find(SList!int(1, 2, 3, 4, 5)[], 4), SList!int(4, 5)[]));
|
|
||||||
assert(find!"a > b"([1, 2, 3, 5], 2) == [3, 5]);
|
|
||||||
|
|
||||||
auto a = [ 1, 2, 3 ];
|
/// Case-insensitive find of a string
|
||||||
assert(find(a, 5).empty); // not found
|
@safe unittest
|
||||||
assert(!find(a, 2).empty); // found
|
{
|
||||||
|
import std.range.primitives;
|
||||||
|
import std.uni : toLower;
|
||||||
|
|
||||||
// Case-insensitive find of a string
|
string[] s = ["Hello", "world", "!"];
|
||||||
string[] s = [ "Hello", "world", "!" ];
|
assert(s.find!((a, b) => toLower(a) == b)("hello") == s);
|
||||||
assert(!find!("toLower(a) == b")(s, "hello").empty);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@safe unittest
|
@safe unittest
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue