@@ -1473,6 +1473,9 @@ Elements of `haystack` are compared with `needle` by using predicate
14731473`pred` with `pred(haystack.front, needle)`.
14741474`find` performs $(BIGOH walkLength(haystack)) evaluations of `pred`.
14751475
1476+ The predicate is passed to $(REF binaryFun, std, functional), and can either accept a
1477+ string, or any callable that can be executed via `pred(element, element)`.
1478+
14761479To _find the last occurrence of `needle` in a
14771480$(REF_ALTTEXT bidirectional, isBidirectionalRange, std,range,primitives) `haystack`,
14781481call `find(retro(haystack), needle)`. See $(REF retro, std,range).
@@ -1664,38 +1667,29 @@ if (isInputRange!InputRange &&
16641667// /
16651668@safe unittest
16661669{
1667- import std.algorithm.comparison : equal;
1668- import std.container : SList;
1669- import std.range ;
1670- import std.range.primitives : empty;
1670+ import std.range.primitives ;
16711671
1672- auto arr = assumeSorted! " a < b" ([1 , 2 , 4 , 4 , 4 , 4 , 5 , 6 , 9 ]);
1673- assert (find(arr, 4 ) == assumeSorted! " a < b" ([4 , 4 , 4 , 4 , 5 , 6 , 9 ]));
1674- assert (find(arr, 1 ) == arr);
1675- assert (find(arr, 9 ) == assumeSorted! " a < b" ([9 ]));
1676- assert (find! " a > b" (arr, 4 ) == assumeSorted! " a < b" ([5 , 6 , 9 ]));
1677- assert (find! " a < b" (arr, 4 ) == arr);
1678- assert (find(arr, 0 ).empty());
1679- assert (find(arr, 10 ).empty());
1680- assert (find(arr, 8 ).empty());
1681-
1682- auto r = assumeSorted! " a > b" ([10 , 7 , 3 , 1 , 0 , 0 ]);
1683- assert (find(r, 3 ) == assumeSorted! " a > b" ([3 , 1 , 0 , 0 ]));
1684- assert (find! " a > b" (r, 8 ) == r);
1685- assert (find! " a < b" (r, 5 ) == assumeSorted! " a > b" ([3 , 1 , 0 , 0 ]));
1672+ auto arr = [1 , 2 , 4 , 4 , 4 , 4 , 5 , 6 , 9 ];
1673+ assert (arr.find(4 ) == [4 , 4 , 4 , 4 , 5 , 6 , 9 ]);
1674+ assert (arr.find(1 ) == arr);
1675+ assert (arr.find(9 ) == [9 ]);
1676+ assert (arr.find! ((a, b) => a > b)(4 ) == [5 , 6 , 9 ]);
1677+ assert (arr.find! ((a, b) => a < b)(4 ) == arr);
1678+ assert (arr.find(0 ).empty);
1679+ assert (arr.find(10 ).empty);
1680+ assert (arr.find(8 ).empty);
16861681
16871682 assert (find(" hello, world" , ' ,' ) == " , world" );
1688- assert (find([1 , 2 , 3 , 5 ], 4 ) == []);
1689- assert (equal(find(SList! int (1 , 2 , 3 , 4 , 5 )[], 4 ), SList! int (4 , 5 )[]));
1690- assert (find! " a > b" ([1 , 2 , 3 , 5 ], 2 ) == [3 , 5 ]);
1683+ }
16911684
1692- auto a = [ 1 , 2 , 3 ];
1693- assert (find(a, 5 ).empty); // not found
1694- assert (! find(a, 2 ).empty); // found
1685+ // / Case-insensitive find of a string
1686+ @safe unittest
1687+ {
1688+ import std.range.primitives ;
1689+ import std.uni : toLower;
16951690
1696- // Case-insensitive find of a string
1697- string [] s = [ " Hello" , " world" , " !" ];
1698- assert (! find! (" toLower(a) == b" )(s, " hello" ).empty);
1691+ string [] s = [" Hello" , " world" , " !" ];
1692+ assert (s.find! ((a, b) => toLower(a) == b)(" hello" ) == s);
16991693}
17001694
17011695@safe unittest
0 commit comments