Comprehensions
Learn about comprehension, its scope, and its returned values.
We'll cover the following...
When we’re writing functional code, we often map and filter collections of things. To make our life easier (and our code easier to read), Elixir provides a general-purpose shortcut for this: the comprehension.
The idea of comprehension is fairly simple: given one or more collections, extract all combinations of values from each, optionally filter the values, and then generate a new collection using the remaining values.
The general syntax for comprehensions is deceptively simple: result = for generator or filter... [, into: value ], do: expression
.
Let’s see a couple of basic examples before we get into the details.
iex> for x <- [
...