@@ -1473,6 +1473,9 @@ Elements of `haystack` are compared with `needle` by using predicate
1473
1473
`pred` with `pred(haystack.front, needle)`.
1474
1474
`find` performs $(BIGOH walkLength(haystack)) evaluations of `pred`.
1475
1475
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
+
1476
1479
To _find the last occurrence of `needle` in a
1477
1480
$(REF_ALTTEXT bidirectional, isBidirectionalRange, std,range,primitives) `haystack`,
1478
1481
call `find(retro(haystack), needle)`. See $(REF retro, std,range).
@@ -1664,38 +1667,29 @@ if (isInputRange!InputRange &&
1664
1667
// /
1665
1668
@safe unittest
1666
1669
{
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 ;
1671
1671
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);
1686
1681
1687
1682
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
+ }
1691
1684
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;
1695
1690
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);
1699
1693
}
1700
1694
1701
1695
@safe unittest
0 commit comments