4 ms·
That is one thing you could do to merge dicts. To expand on my last paragraph above, I think I would imagine the following operations (stupid syntax): a & b
by dan-robertson 8y ago
That is one thing you could do to merge dicts. To expand on my last paragraph above, I think I would imagine the following operations (stupid syntax):
a & b = { k: (a[k], b[k]) for k in a.keys() | b.keys() }
a | b = { k: (a.get(k, None), b.get(k, None)) for k in a.keys() | b.keys() }
a |& b = { k: (a.get(k,None), v) for k, v in b.items() }
a &| b = { k: (v, b.get(k,None)) for k, v in a.items() }
a |_| b = { k: only(a,b,k) for k in a.keys() | b.keys() }
def only(a,b,k):
if k in a && k in b:
throw DuplicateKey(a,b,k)
elseif k not in a && k not in b:
assert(false)
elseif k in a:
return a[k]
else:
return b[k]
This doesn’t work well if values can be None so maybe instead of pairs there should be objects Left(x), Right(y), and Both(x,y)