[erlang-questions] Merging maps with a fun like dict:merge/3
Richard A. O'Keefe
Thu Aug 27 02:50:29 CEST 2015
Merging whatevers has three cases:
- this is in the left but not the right
- this is in the right but not the left
- this is in both with these values
That is, given
data Map k v
merge :: (k -> v -> Maybe r) -> -- left handler
(k -> w -> Maybe r) -> -- right handler
(k -> v -> w -> Maybe r) -> -- union handler
Map k v -> -- left map
Map k w -> -- right map
Map k r -- result map
The left handler is given a key and value from the left map
and the assurance that the key is not in the right map.
It either returns Nothing, discarding this element,
or Just r meaning that k :-> r should be in the result.
The other handlers work in the same way.
Of course there can be specialisations, but this is what I
would expect as the foundation.
The frames proposal didn't include such a function because
a good record substitute and a good dictionary are not the
same kind of animal and should not be mixed up.
More information about the erlang-questions