Jump to page: 1 26  
Page
Thread overview
Feature to get or add value to an associative array.
Apr 15, 2018
Giles Bathgate
Apr 15, 2018
Giles Bathgate
Apr 16, 2018
Jordan Wilson
Apr 16, 2018
Giles Bathgate
Apr 16, 2018
bauss
Apr 16, 2018
user1234
Apr 18, 2018
Giles Bathgate
Apr 18, 2018
Jordan Wilson
Apr 19, 2018
Giles Bathgate
Apr 20, 2018
Jonathan M Davis
Apr 20, 2018
Giles Bathgate
Apr 16, 2018
JN
Apr 16, 2018
Giles Bathgate
Apr 17, 2018
Cym13
Apr 17, 2018
Nicholas Wilson
Apr 17, 2018
Giles Bathgate
Apr 17, 2018
Giles Bathgate
Apr 17, 2018
Giles Bathgate
Apr 17, 2018
Nick Treleaven
Apr 17, 2018
Giles Bathgate
Apr 17, 2018
MrSmith
Apr 17, 2018
Giles Bathgate
Apr 17, 2018
Giles Bathgate
Apr 17, 2018
Giles Bathgate
Apr 18, 2018
Nick Treleaven
Apr 18, 2018
Giles Bathgate
Apr 20, 2018
Nick Treleaven
Apr 20, 2018
Giles Bathgate
Apr 18, 2018
ag0aep6g
Apr 20, 2018
Nick Treleaven
Apr 20, 2018
ag0aep6g
Apr 20, 2018
Jonathan M Davis
Apr 20, 2018
ag0aep6g
Apr 20, 2018
Uknown
Apr 20, 2018
Uknown
Why don't lazy parameters bind to delegates? Was: Feature to get or add value to an associative array.
Apr 20, 2018
Jonathan M Davis
Apr 21, 2018
Jonathan M Davis
Apr 20, 2018
Giles Bathgate
Apr 20, 2018
Jonathan M Davis
Apr 20, 2018
Giles Bathgate
Apr 20, 2018
Jonathan M Davis
Apr 20, 2018
Giles Bathgate
Apr 21, 2018
Jonathan M Davis
Apr 24, 2018
Andrea Fontana
Apr 25, 2018
Giles Bathgate
April 15, 2018
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

The function provides a means to get a value corresponding to the key, but if the value doesn't exist it will evaluate the lazy argument to create a new value, add this to the associative array and then return it.

```
auto p = lookup.getOrAdd("giles", new Person);
p.eyeColor = Color.Brown;
//...
assert("giles" in lookup);
```

Corresponding documentation added in https://github.com/dlang/dlang.org/pull/2343

Implementation details:

Traditionally the above example could be done like so:
```
auto p = "giles" in lookup;
if (p is null) {
    p = new Person;
    lookup["giles"] = p;
}
p.eyeColor = Color.Brown;
//...
assert("giles" in lookup);
```

I find this later code clunky and it requires two hashes/lookups. The proposed implementation adds support directly to rt/aaA.d to avoid this. I should also point out that the allocation of a new Person in the example is trivial, but it might often be the case that the person instance is created by fetching a record from a db, or an Image loaded from disk, or a movie downloaded from the internet.
April 15, 2018
On Sunday, 15 April 2018 at 22:52:47 UTC, Giles Bathgate wrote:
> I am proposing a new function called getOrAdd in

I posted details of the PR here because at least 2 people do not like the name getOrAdd (and one of those people being myself)

Time for a bikeshed discussion...


April 16, 2018
On Sunday, 15 April 2018 at 22:55:41 UTC, Giles Bathgate wrote:
> On Sunday, 15 April 2018 at 22:52:47 UTC, Giles Bathgate wrote:
>> I am proposing a new function called getOrAdd in
>
> I posted details of the PR here because at least 2 people do not like the name getOrAdd (and one of those people being myself)
>
> Time for a bikeshed discussion...

I think Adding a Yes.add flag to the existing get makes sense, but property functions only have max 2 args.

Soooo...getOrAdd or getOrSet would be my pick.

Jordan
April 16, 2018
On Monday, 16 April 2018 at 03:42:18 UTC, Jordan Wilson wrote:
> I think Adding a Yes.add flag to the existing get makes sense

That's an interesting idea. I forgot to mention that getOrAdd is a mutable function whereas get is inout, so that might also be problematic.


April 16, 2018
On Monday, 16 April 2018 at 03:42:18 UTC, Jordan Wilson wrote:
> On Sunday, 15 April 2018 at 22:55:41 UTC, Giles Bathgate wrote:
>> On Sunday, 15 April 2018 at 22:52:47 UTC, Giles Bathgate wrote:
>>> I am proposing a new function called getOrAdd in
>>
>> I posted details of the PR here because at least 2 people do not like the name getOrAdd (and one of those people being myself)
>>
>> Time for a bikeshed discussion...
>
> I think Adding a Yes.add flag to the existing get makes sense, but property functions only have max 2 args.
>
> Soooo...getOrAdd or getOrSet would be my pick.
>
> Jordan

I'm opposed to flags. I hate the whole "yes" "no" thing when it comes to such things, just like the "true" "false"... It's a no go for me.

A function name that fits would be perfect in my opinion.

April 16, 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

I am not sure if it's a good idea to combine accessor and insertion in a same method call. Is it really such a big overhead to do "if in assocarray then take else create new"? It's only one additional "in", but makes the code more explicit and clear. I think in most cases, you will want to check if you are dealing with a fetched object or a default created one, so that will complicate the function even further.

April 16, 2018
On Monday, 16 April 2018 at 12:41:07 UTC, JN wrote:
> It's only one additional "in", but makes the code more explicit and clear. I think in most cases, you will want to check if you are dealing with a fetched object or a default created one, so that will complicate the function even further.

You can still use a combination of `in` and the update syntax if you want, this doesn't take that away. In the future, for a concurrent implementation of associative arrays, a way of getting or adding an element as an atomic operation becomes more important. Either way the beauty of hashed based lookups is that they average O(1), seems a shame to double that up for no reason ;)
April 16, 2018
On Sunday, 15 April 2018 at 22:55:41 UTC, Giles Bathgate wrote:
> On Sunday, 15 April 2018 at 22:52:47 UTC, Giles Bathgate wrote:
>> I am proposing a new function called getOrAdd in
>
> I posted details of the PR here because at least 2 people do not like the name getOrAdd (and one of those people being myself)
>
> Time for a bikeshed discussion...

It's hard to name... I cant find better than "valueOrDefault"
April 17, 2018
On Monday, 16 April 2018 at 18:59:54 UTC, Giles Bathgate wrote:
> On Monday, 16 April 2018 at 12:41:07 UTC, JN wrote:
>> It's only one additional "in", but makes the code more explicit and clear. I think in most cases, you will want to check if you are dealing with a fetched object or a default created one, so that will complicate the function even further.
>
> You can still use a combination of `in` and the update syntax if you want, this doesn't take that away. In the future, for a concurrent implementation of associative arrays, a way of getting or adding an element as an atomic operation becomes more important. Either way the beauty of hashed based lookups is that they average O(1), seems a shame to double that up for no reason ;)

"in" returns a pointer to the object, there'es not double lookup necessary:

    // if we don't know .get(key, default) exists
    auto ptr   = key in aa;
    auto value = ptr ? *ptr : default;

    // to set default value on the fly
    auto value = ptr ? *ptr : *ptr = default;

is a new flag/method really that necessary? In my experience if you have trouble naming it you haven't found its true purpose yet.
April 17, 2018
On Tuesday, 17 April 2018 at 00:04:32 UTC, Cym13 wrote:
>     auto value = ptr ? *ptr : *ptr = default;

That is going to crash if ptr is null.

> is a new flag/method really that necessary? In my experience if you have trouble naming it you haven't found its true purpose yet.

I think `getOrInsert` is a good name for this function
« First   ‹ Prev
1 2 3 4 5 6