In Elixir, protocols are a way to achieve polymorphism. The Enum
and Stream
modules work on data types that implement the Enumerable
protocol, so the behavior of both modules becomes similar. In this context, polymorphism might be perceived as a common API to interact with different modules.
All Enum
module functions accept a collection as one of the arguments, and two very common operations in collections are map and reduce. With map, we perform some kind of operation on every element of a given collection, and with reduce, the whole collection is reduced into a value.