Feature to get or add value to an associative array.

Andrea Fontana nospam at example.com
Tue Apr 24 09:09:37 UTC 2018


On Sunday, 15 April 2018 at 22:52:47 UTC, Giles Bathgate wrote:
> Hi,
>
> I wanted a way to lazily insert a value into an associative 
> array, and I am proposing a new function called getOrAdd in  
> https://github.com/dlang/druntime/pull/2162

Yes please :)

Just one question: does this work for value-types?

This is a pretty common case for me (simplified and not optimized 
version):

size_t[string] counter;
...
foreach(e; elements)
{
    if (e.something !in counter) counter[e.something] = 0;
    else counter[e.something]++;
}

So will this work?

foreach(e; elements)
    counter.update(e.something, { return 0; }, (size_t v) { return 
v+1; })

Andrea


Andrea


More information about the Digitalmars-d mailing list