Last active
June 16, 2019 15:19
-
-
Save Adzz/a9e46fe43dd50a0944fa196f122d9931 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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