Why any! with map! is not working here

Marco de Wild mdwild at sogyo.nl
Thu Jun 6 09:07:40 UTC 2019


On Thursday, 6 June 2019 at 09:01:11 UTC, rnd wrote:
> I am trying to check if any character in the string is > 127 by 
> following function:
>
> import std.algorithm.searching;
> import std.algorithm.iteration;
> bool isBinary(char[] ss){
>   return (any!(map!(a => a > 127)(ss)));
> }
>
> However, I am getting this error:
>
> Error: variable ss cannot be read at compile time
>
> I also tried:
>
> bool isBinary(char[] ss){
> 	return (any!(ss.map!(a => a > 127)));
> }
>
>
> But I get error:
>
> Error: template identifier map is not a member of variable 
> mysrcfile.isBinary.ss
>
>
> How can this be corrected?

Removing the ! makes it work:
```
import std.algorithm.searching;
import std.algorithm.iteration;
bool isBinary(char[] ss){
   return (any(map!(a => a > 127)(ss)));
}
```

`any` takes a runtime argument (a range) and optionally a 
template argument.

You can also use UFCS[0]
```
import std.algorithm.searching;
import std.algorithm.iteration;
bool isBinary(char[] ss){
   return ss.map!(a => a > 127).any;
}
```
to make it more obvious to see what is happening. It can be quite 
confusing to mix chains of template and runtime arguments.

[0] 
https://tour.dlang.org/tour/en/gems/uniform-function-call-syntax-ufcs


More information about the Digitalmars-d-learn mailing list