Re: best idiom for insert if not present; else use value in assoc array

2013-02-09 Thread Dan
On Saturday, 9 February 2013 at 00:54:58 UTC, Andrej Mitrovic wrote: Feel free to file an enhancement request. http://d.puremagic.com/issues/show_bug.cgi?id=9491 Hopefully that is clear.

Re: best idiom for insert if not present; else use value in assoc array

2013-02-08 Thread Andrej Mitrovic
On 2/7/13, Dan wrote: > I believe this would have to be done at the level of > AssociativeArray and can not be done as a helper function, since > this set helper is still doing 3 hashes/lookups. Feel free to file an enhancement request.

Re: best idiom for insert if not present; else use value in assoc array

2013-02-08 Thread Ary Borenszweig
On 2/7/13 5:20 PM, Dan wrote: For an associative array, what is the best idiom that allows for checking if a key exists in the AA. If it does do something with the value, if not initialize the value and then do something with it. In this code: http://dpaste.dzfl.pl/daab318f How would this piece

Re: best idiom for insert if not present; else use value in assoc array

2013-02-07 Thread Dan
On Thursday, 7 February 2013 at 20:46:31 UTC, Andrej Mitrovic wrote: auto ref set(Hash, Key, Val)(Hash hash, Key key, Val val) if (isAssociativeArray!Hash && is(Key : KeyType!Hash) && is(Val : ValueType!Hash)) { if (auto res = key in hash) { return res; }

Re: best idiom for insert if not present; else use value in assoc array

2013-02-07 Thread Jonathan M Davis
On Thursday, February 07, 2013 21:46:20 Andrej Mitrovic wrote: > On 2/7/13, Jonathan M Davis wrote: > > I don't think that such a function exists > > though. > > UFCS would do it: > > import std.traits; > > auto ref set(Hash, Key, Val)(Hash hash, Key key, Val val) > if (isAssociativeArray!Hash

Re: best idiom for insert if not present; else use value in assoc array

2013-02-07 Thread bearophile
Jonathan M Davis: But I certainly agree that it would be nice if there were a function that inserted the element if it wasn't already there and then returns it regardless of whether it was there or not. I don't think that such a function exists though. Python dicts have a method that I don'

Re: best idiom for insert if not present; else use value in assoc array

2013-02-07 Thread Andrej Mitrovic
On 2/7/13, Jonathan M Davis wrote: > I don't think that such a function exists > though. UFCS would do it: import std.traits; auto ref set(Hash, Key, Val)(Hash hash, Key key, Val val) if (isAssociativeArray!Hash && is(Key : KeyType!Hash) && is(Val : ValueType!Hash)) { if

Re: best idiom for insert if not present; else use value in assoc array

2013-02-07 Thread Jonathan M Davis
On Thursday, February 07, 2013 21:20:46 Dan wrote: > For an associative array, what is the best idiom that allows for > checking if a key exists in the AA. If it does do something with > the value, if not initialize the value and then do something with > it. > > In this code: http://dpaste.dzfl.pl

best idiom for insert if not present; else use value in assoc array

2013-02-07 Thread Dan
For an associative array, what is the best idiom that allows for checking if a key exists in the AA. If it does do something with the value, if not initialize the value and then do something with it. In this code: http://dpaste.dzfl.pl/daab318f How would this piece be improved? It looks like