Skip to content

Commit 585ddbe

Browse files
authored
[std.algorithm] Improve canFind docs (#8843)
Minor tweaks. Fix wrong `LREF`. Also fix wrong `REF` to `among`.
1 parent 870eb5d commit 585ddbe

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

std/algorithm/searching.d

+19-17
Original file line numberDiff line numberDiff line change
@@ -2536,13 +2536,13 @@ was successful.
25362536
For more information about `pred` see $(LREF find).
25372537
25382538
See_Also:
2539-
$(REF among, std,algorithm,comparison) for checking a value against multiple possibilities.
2539+
$(REF among, std,algorithm,comparison) for checking a value against multiple arguments.
25402540
+/
25412541
template canFind(alias pred="a == b")
25422542
{
25432543
/++
2544-
Returns `true` if and only if any value `v` found in the
2545-
input range `range` satisfies the predicate `pred`.
2544+
Returns `true` if and only if `pred(e)` is true for any value `e` in the
2545+
input range `range`.
25462546
Performs (at most) $(BIGOH haystack.length) evaluations of `pred`.
25472547
+/
25482548
bool canFind(Range)(Range haystack)
@@ -2565,11 +2565,11 @@ template canFind(alias pred="a == b")
25652565
Returns the 1-based index of the first needle found in `haystack`. If no
25662566
needle is found, then `0` is returned.
25672567
2568-
So, if used directly in the condition of an if statement or loop, the result
2568+
So, if used directly in the condition of an `if` statement or loop, the result
25692569
will be `true` if one of the needles is found and `false` if none are
25702570
found, whereas if the result is used elsewhere, it can either be cast to
25712571
`bool` for the same effect or used to get which needle was found first
2572-
without having to deal with the tuple that `LREF find` returns for the
2572+
without having to deal with the tuple that $(LREF find) returns for the
25732573
same operation.
25742574
+/
25752575
size_t canFind(Range, Ranges...)(Range haystack, scope Ranges needles)
@@ -2584,15 +2584,17 @@ template canFind(alias pred="a == b")
25842584
///
25852585
@safe unittest
25862586
{
2587-
assert(canFind([0, 1, 2, 3], 2) == true);
2588-
assert(canFind([0, 1, 2, 3], [1, 2], [2, 3]));
2589-
assert(canFind([0, 1, 2, 3], [1, 2], [2, 3]) == 1);
2590-
assert(canFind([0, 1, 2, 3], [1, 7], [2, 3]));
2591-
assert(canFind([0, 1, 2, 3], [1, 7], [2, 3]) == 2);
2587+
const arr = [0, 1, 2, 3];
2588+
assert(canFind(arr, 2));
2589+
assert(!canFind(arr, 4));
25922590

2593-
assert(canFind([0, 1, 2, 3], 4) == false);
2594-
assert(!canFind([0, 1, 2, 3], [1, 3], [2, 4]));
2595-
assert(canFind([0, 1, 2, 3], [1, 3], [2, 4]) == 0);
2591+
// find one of several needles
2592+
assert(canFind(arr, [1, 2], [2, 3]));
2593+
assert(canFind(arr, [1, 2], [2, 3]) == 1);
2594+
assert(canFind(arr, [1, 7], [2, 3]));
2595+
assert(canFind(arr, [1, 7], [2, 3]) == 2);
2596+
assert(!canFind(arr, [1, 3], [2, 4]));
2597+
assert(canFind(arr, [1, 3], [2, 4]) == 0);
25962598
}
25972599

25982600
/**
@@ -2607,18 +2609,18 @@ template canFind(alias pred="a == b")
26072609
"cardboard"
26082610
];
26092611
assert(!canFind(words, "bees"));
2610-
assert( canFind!((string a, string b) => a.startsWith(b))(words, "bees"));
2612+
assert( canFind!((string elem, string needle) => elem.startsWith(needle))(words, "bees"));
26112613
}
26122614

2613-
/// Search for mutliple items in an array of items (search for needles in an array of hay stacks)
2615+
/// Search for multiple items in an array of items (search for needles in an array of haystacks)
26142616
@safe unittest
26152617
{
26162618
string s1 = "aaa111aaa";
26172619
string s2 = "aaa222aaa";
26182620
string s3 = "aaa333aaa";
26192621
string s4 = "aaa444aaa";
26202622
const hay = [s1, s2, s3, s4];
2621-
assert(hay.canFind!(e => (e.canFind("111", "222"))));
2623+
assert(hay.canFind!(e => e.canFind("111", "222")));
26222624
}
26232625

26242626
@safe unittest
@@ -2736,7 +2738,7 @@ Returns:
27362738
`seq` advanced to the first matching element, or until empty if there are no
27372739
matching elements.
27382740
2739-
See_Also: $(LREF find), $(REF std,algorithm,comparison,among)
2741+
See_Also: $(LREF find), $(REF among, std,algorithm,comparison)
27402742
*/
27412743
InputRange findAmong(alias pred = "a == b", InputRange, ForwardRange)(
27422744
InputRange seq, ForwardRange choices)

0 commit comments

Comments
 (0)