Suggestion: Operator `in` for slices
Stanislav Blinov
stanislav.blinov at gmail.com
Mon Dec 20 14:34:40 UTC 2021
On Monday, 20 December 2021 at 14:21:08 UTC, Paul Backus wrote:
> On Monday, 20 December 2021 at 13:54:56 UTC, Ola Fosheim
> Grøstad wrote:
>> E.g.
>>
>> ```
>> if keyword in ['if', 'while', 'for]
>> ```
>>
>> is a very useful and clear syntactical construct.
>
> ```d
> import std.algorithm.searching;
>
> if (["if", "while", "for"].canFind(keyword)) {
> // ...
> }
> ```
TBH, for that particular case, this looks nicer, and IMO
documents the intent better than the original Pythonism:
```d
import std.algorithm.comparison : among;
if (keyword.among("if", "while", "for")) { /* ... */ }
```
It also doesn't let work go to waste, as it returns a 1-based
index.
More information about the Digitalmars-d
mailing list