range methods on associative arrays

WebFreak001 d.forum at webfreak.org
Sat Jul 2 20:14:10 UTC 2022


a lot of range methods, such as `filter`, `any`, `all`, `count`, 
`each`, etc. would be useful on associative arrays, taking in key 
and value, returning a processed .byKeyValue range.

I would suggest, at least for phobos v2, we should have these 
functions automatically call `.byKeyValue` on maps and there 
should be support for lambdas with 2 arguments there, which 
automatically unwrap key and value (and possibly all tuples 
actually)

What do you think?

```d
map.each!((key, value) { /* like a foreach, but functional style 
*/ });

bool hasId = map.any!((key, value) => key == "id" && value !is 
null);
```

for this I think the implementation would basically boil down to:

- implicitly call `.byKeyValue` in the map-accepting range methods
- allow tuples and the KeyValue pair to be extended into multiple 
parameters on CT lambdas that have multiple arguments

Users wanting to only use keys or only values can still use 
.byKey or .byValue.


More information about the Digitalmars-d mailing list