Added merge_with
This commit is contained in:
parent
8f69581893
commit
79c503678e
|
@ -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:
|
||||
"""
|
||||
|
|
Loading…
Reference in a new issue