Enumerable
Explore how to make custom data types enumerable in Elixir by implementing the Enumerable protocol. Understand the core functions—reduce, count, member?, and slice—and see how they enable you to use the Enum module with your own collections. This lesson guides you through practical examples of implementing these functions for a MIDI data structure.
We'll cover the following...
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 ...