range methods on associative arrays
WebFreak001
d.forum at webfreak.org
Sat Jul 2 21:51:47 UTC 2022
On Saturday, 2 July 2022 at 21:18:06 UTC, Steven Schveighoffer
wrote:
> 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
arrays/slices are also not ranges and get special-cased (or at
least have compatibility functions in std.range.primitives) to be
used with range code, I don't see any reason why not to do this
with maps as well, which are a first class feature of the
language.
More information about the Digitalmars-d
mailing list