Last active
June 16, 2019 14:41
-
-
Save Adzz/09d5b2136629477bf6ddda04e668d03d 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
defprotocol Zip do | |
def apply(collection_1, collection_2, operation) | |
end | |
# We can implement it for lists: | |
defimpl Zip, for: List do | |
def apply(a, b, calculate) do | |
Enum.zip(a, b) | |
|> Enum.map(fn {a, b} -> calculate.(a, b) end) | |
end | |
end | |
# Now our zip works: | |
Zip.apply([1, 2], [3, 4], fn x, y -> x + y end) #=> [4, 6] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment