Created
September 4, 2022 21:56
-
-
Save rugyoga/508a4dc3a9f75ff2a9d7df5666abc072 to your computer and use it in GitHub Desktop.
Take the delta of two maps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@spec delta_map(path(), map(), map()) :: [delta()] | |
def delta_map(path, a, b) do | |
a_keys = MapSet.new(Map.keys(a)) | |
b_keys = MapSet.new(Map.keys(b)) | |
common = MapSet.intersection(a_keys, b_keys) | |
a_only = a_keys |> MapSet.difference(common) | |
b_only = b_keys |> MapSet.difference(common) | |
Enum.flat_map(common, fn key -> delta([key | path], a[key], b[key]) end) ++ | |
Enum.flat_map(a_only, fn key -> [{[key | path], {:delete, a[key]}}] end) ++ | |
Enum.flat_map(b_only, fn key -> [{[key | path], {:add, b[key]}}] end) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment