Using the Enum Module
Learn about higher-order functions in Elixir's Enum module.
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"], &(&1 > "b"))
#Output ->["c", "d"]
Get hands-on with 1400+ tech skills courses.