ðEnumerables
Many of the common recursive patterns are provided as functions from the Enum
module. These are functions that are often chained with one another and reduces the code duplication necessary in your codebase. These patterns often involve operating on "enumerables" (such as lists, tuples, and maps) and produce some result.
Enum functions
The Enum
module in Elixir is rather extensive and provides many utility functions that would otherwise require dedicated recursive functions. We will highlight just a few notable ones that you will use most often:
Enum.all?
: returnstrue
iff the entire enumerable istrue
or satisfies a given condition based on a givenfun
Enum.any?
: returnstrue
iff any element in the enumerable istrue
or satisfies a given condition based on a givenfun
Enum.at
: returns the element at a givenindex
with a default value ifindex
is out of bounds (nil
by default)Enum.filter
: returns the filtered enumerable after applying a given predicateEnum.map
: returns the mapped enumerable after applying a given transformation functionEnum.flat_map
: returns the mapped enumerable after applying a given transformation function and flattens any first-level nested enumerablesEnum.sort
: returns the sorted enumerable
There are many more functions that the Enum
module provides. Feel free to read the documentation for more information.
Function chaining
You may notice that applying Enum
functions (or any function for that matter) often requires chaining, where you pass the output of one function call as the input to another till the final output is produced.
While you can nest these function calls as such:
It becomes very messy once you have more than two nested function calls. Instead, you can use the pipe operator (|>
) to perform function chaining (the equivalent of the above example):
Using the pipe operator helps to tidy up the code and reduce the clutter when performing function chaining.
Streams
Enum
functions perform computation eagerly, i.e. the transformation function is called for every element of the enumerable immediately in a map
call. However, this can be incredibly costly as the enumerable might be an infinite stream or just a very large list with the transformation being extremely costly.
Thus, Elixir supports lazy computation of commonly used Enum
functions through the Stream
module. This guide will not cover streams in-depth as it is not used in this guide, but it is good to give the documentation of Stream
a read.
Last updated