In Elixir, protocols are a way to achieve polymorphism. The
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.
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.
For this recipe, we will use a new IEx session. To start it, type
iex in your console.
To perform map and reduce on a collection, ...