Using the Enum Module
Explore how to apply Elixir's Enum module to handle common data operations with higher-order functions. Learn to manipulate lists, maps, and group data efficiently, enhancing your functional programming skills with reusable and flexible code.
We'll cover the following...
Higher-order functions
The each, map, reduce, and filter list operations are used in almost all of the programming tasks we do with lists. Thanks to Elixir’s core team, we don’t need to write these higher-order functions every time we start a new Elixir project because they’re available in the Enum module. We wrote all these functions to understand how to create higher-order functions. From now on, we’ll use them directly from the Enum module.
Now we’ll experiment with more useful higher-order functions from that module, starting with ones we’ve built. Try this in your IEx below:
iex> Enum.each(["dogs", "cats", "flowers"], &(IO.puts String.upcase(&1)))
#Output ->
DOGS
CATS
FLOWERS
iex> Enum.map(["dogs", "cats", "flowers"], &String.capitalize/1)
#Output -> ["Dogs", "Cats", "Flowers"]
iex> Enum.reduce([10, 5, 5, 10], 0, &+/2)
#Output -> 30
iex> Enum.filter(["a", "b", "c", "d"], ...