Skip to content

Instantly share code, notes, and snippets.

@Adzz
Last active June 16, 2019 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Adzz/a9e46fe43dd50a0944fa196f122d9931 to your computer and use it in GitHub Desktop.
Save Adzz/a9e46fe43dd50a0944fa196f122d9931 to your computer and use it in GitHub Desktop.
defimpl Zip, for: Map do
def apply(a, b, operation) do
# We take the all the keys from b that are also in a
intersection = Map.take(b, Map.keys(a))
Enum.reduce(a, %{}, fn {key, value}, acc ->
Map.put(acc, key, operation.calculate(value, Map.fetch!(intersection, key)))
end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment