Basically, I'm in the process of writing a more-specific hash table implementation, based on the existing one, but more tailored to my needs and with some functions that I consider "missing".
I'm not sure whether I'll end up using it in the end, but I'm still trying different things out.
One of the things that I've been absolutely looking for is a function that with a single call updates a specific key-value:
Have I missed something?
proc editIfExists[A, B](t: Table[A, B], key: A, val: B) =
if key in t:
t[key] = val
I think @Yardanico's answer nailed it. I'll try it out, but - judging from its implementation - I think that's exactly what I've been looking for.
My problem with @asteroid_den's approach is that there are 2 lookups (one for k in t and another one for t[key]=). And that's exactly what I'm trying to avoid.
In any case, thanks to both for helping out! Really appreciated! :)
With std/tables you can also use mgetOrPut with a small helper proc to do the update as in t.mgetOrPut(key, initial).edit(args) with a proc edit(cell: var V, args) (where args here will depend on your use case).
You may also consider an editOrInit which takes a found & missing clause as in https://github.com/c-blake/adix/blob/master/adix/lptabz.nim (or consider that package).