range methods on associative arrays
Steven Schveighoffer
schveiguy at gmail.com
Sat Jul 2 21:18:06 UTC 2022
On 7/2/22 4:14 PM, 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.
This idea misunderstands what a range is.
In order for an AA to be considered a "range", each popFront should
remove the element from the AA.
We don't want that. Just use the range accessors.
-Steve
More information about the Digitalmars-d
mailing list