std.algorithm.canFind behavior difference between arrays and elements

Arun Chandrasekaran aruncxy at gmail.com
Fri Dec 7 19:13:37 UTC 2018


On Friday, 7 December 2018 at 19:08:05 UTC, Arun Chandrasekaran 
wrote:
> On Friday, 7 December 2018 at 18:57:48 UTC, Dennis wrote:
>> On Friday, 7 December 2018 at 18:51:27 UTC, Arun 
>> Chandrasekaran wrote:
>>> Why is there a difference in the behavior?
>>
>> Your first assert expression is looking for a string in a 
>> larger string, your second expression looks for hay which is 
>> not a string but a string[]. To flatten the array, use:
>>
>> assert(canFind(hay.join, "111"));
>
> This succeeds.
>
> assert(canFind(hay, "aaa222aaa"));
>
> So the difference in the behaviour is caused by canFind 
> checking for equality when string[] is passed. Is this the 
> expected behaviour? I wouldn't want to join the array, for the 
> array could be really big.

Actually, canFind documentation is perfect. 
https://dlang.org/phobos/std_algorithm_searching.html#.canFind

assert( canFind!((string a, string b) => a.startsWith(b))(words, 
"bees"));

Thanks for the help anyways!


More information about the Digitalmars-d-learn mailing list