Find a char among string (string.findAmong.char)
jfondren
julian.fondren at gmail.com
Mon Jul 5 18:53:27 UTC 2021
On Monday, 5 July 2021 at 18:45:10 UTC, BoQsc wrote:
> I get an error when I try to find that letter is among alphabet.
>> onlineapp.d(13): Error: template
>> `std.algorithm.searching.findAmong` cannot deduce function
>> from argument types `!()(immutable(char), immutable(string))`,
>> candidates are:
>> /dlang/dmd/linux/bin64/../../src/phobos/std/algorithm/searching.d(2694): `findAmong(alias pred = "a == b", InputRange, ForwardRange)(InputRange seq, ForwardRange choices)`
>> with `pred = "a == b",
>> InputRange = immutable(char),
>> ForwardRange = string`
>> must satisfy the following constraint:
>> ` isInputRange!InputRange`
>
>
> This is the code: You can run it at:
> https://run.dlang.io/is/5cvuUZ
> import std;
> void main()
> {
> alias alphabet = letters;
> char[26] letters = ['a','b', 'c', 'd', 'e',
> 'f', 'g', 'h', 'i', 'j',
> 'k', 'l', 'm', 'n', 'o',
> 'p', 'q', 'r', 's', 't',
> 'u', 'v', 'w', 'x', 'y', 'z'];
>
> string wordExample = "Book.";
> foreach (letter; wordExample){
> if (letter.findAmong(alphabet)){
> write("found");
> }
> write(letter);
> }
>
> }
If you replace the findAmong call with
`[letter].findAmong(alphabet)`, this works.
Both arguments to findAmong need to be ranges; you're calling it
here against an immutable(char). As it says in the error:
```
from argument types `!()(immutable(char), immutable(string))`
```
immutable(char) looks a lot like immutable(char)[], but the
latter is a string and the former is just a singular char.
More information about the Digitalmars-d-learn
mailing list