range methods on associative arrays

bauss jj_1337 at live.dk
Mon Jul 4 06:20:43 UTC 2022


On Saturday, 2 July 2022 at 20:14:10 UTC, WebFreak001 wrote:
> 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.

I think this confuses an associative array/hashmap with a "list" 
of key-value pairs; which isn't the same.

An associative array isn't really range-like in nature, if it was 
up to me then you shouldn't even be able to natively iterate over 
it in a foreach like you can now, but that's just me being too 
strict for the norm I guess.


More information about the Digitalmars-d mailing list