O'Reilly logo

Elixir Cookbook by Paulo A Pereira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mapping and reducing enumerables

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.

Getting ready

For this recipe, we will use a new IEx session. To start it, type iex in your console.

How to do it…

To perform map and reduce on a collection, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required