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

bearophile bearophileHUGS at lycos.com
Thu Feb 7 12:47:45 PST 2013


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't use often that seems 
related to what you say:

setdefault(key[, default])

     If key is in the dictionary, return its value. If not, insert 
key with a value of default and return default. default defaults 
to None.


Adding some other AA function to object module is possible.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list