Enumerable
Learn the Enumerable built-in protocol of Elixir.
We'll cover the following...
The Enumerable
protocol is the basis of all the functions in the Enum
module.
Any type implementing it can be used as a collection argument to Enum
functions.
We’re going to implement Enumerable
for our Midi
structure, so we’ll need to wrap the implementation in something like this:
defimpl Enumerable, for: Midi do
# ...
end
Major functions
The protocol is defined in terms of four functions: count
, member?
, reduce
, and slice
, as shown below:
defprotocol Enumerable do
def count(collection)
def member?(collection, value)
def reduce(collection, acc, fun)
def slice(collection)
end
The count
function returns the number of elements in the ...