Added merge_with

This commit is contained in:
Emi Simpson 2023-03-30 19:20:07 -04:00
parent 8f69581893
commit 79c503678e
Signed by: Emi
GPG Key ID: A12F2C2FFDC3D847
1 changed files with 12 additions and 0 deletions

View File

@ -205,6 +205,18 @@ fst = indx(0)
snd = indx(1)
"Get the second element of a tuple/sequence"
def merge_with(conflict: Callable[[B, C], D], m1: Mapping[A, B], m2: Mapping[A, C]) -> Mapping[A, B | C | D]:
"""
Merge two mappings, handling conflicts with special behaviour.
>>> merge_with(lambda a, b: a - b, {'a': 10, "b": 20, "c": 30}, {"b": 2, "c": 3, "d": 4}) == {'a': 10, 'b': 18, 'c': 27, 'd': 4}
True
"""
return {
key: ((conflict(m1[key], m2[key]) if key in m2 else m1[key]) if key in m1 else m2[key])
for key in (m1.keys() | m2.keys())
}
# Semantic Editor Combinators
class SemEdComb:
"""