Skip to content

Instantly share code, notes, and snippets.

@Adzz
Last active June 16, 2019 14:41
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/09d5b2136629477bf6ddda04e668d03d to your computer and use it in GitHub Desktop.
Save Adzz/09d5b2136629477bf6ddda04e668d03d to your computer and use it in GitHub Desktop.
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