I am resubmitting the proposal from earlier today with more context and more focus on the important parts. Some concerns and praises stayed in the old thread but feel free to drop them again and I will gladly follow-up.
Hi everyone,
This is a proposal for introducing for let
and for reduce
comprehensions. This proposal is divided in three parts:
-
Problem statement
-
A tour into
for
-
Conclusion
Problem statement
I have been on the record a couple times saying that, while some problems are more cleanly solved with recursion, there is a category of problems that are much more elegant with imperative loops. One of those problems have been described in the ānested-map-reduce-traversalā repository, with solutions available in many different languages. Please read the problem statement in said repository, as I will assume from now on that you are familiar with it.
Personally speaking, the most concise and clear solution is the Python one, which I reproduce here:
section_counter = 1
lesson_counter = 1
for section in sections:
if section["reset_lesson_position"]:
lesson_counter = 1
section["position"] = section_counter
section_counter += 1
for lesson in section["lessons"]:
lesson["position"] = lesson_counter
lesson_counter += 1
There are many things that make this solution clear:
- Reassignment
- Mutability
- Sensitive whitespace
Letās compare it with the Elixir solution I wrote and personally prefer. I am pasting an image below which highlights certain aspects:
-
Lack of reassignment: in Elixir, we canāt reassign variables, we can only rebind them. The difference is, when you do
var = some_value
inside aif
,for
, etc, the value wonāt āleakā to the outer scope. This implies two things in the snippet above:- We need to use
Enum.map_reduce/3
and pass the state in and out (highlighted in red) - When resetting the lesson counter, we need both sides of the conditional (hihhlighted in yellow)
- We need to use
-
Lack of mutability: even though we set the lesson counter inside the inner
map_reduce
, we still need to update the lesson inside the session (highlighted in green) -
Lack of sensitive whitespace: we have two additional lines with
end
in them (highlighted in blue)
As you can see, do-end blocks add very litte noise to the final solution compared to sensitive whitespace. In fact, the only reason while I brought it up is so we can confidentaly discard it from the discussion from now on. And also because there is zero chance of the language suddenly becoming whitespace sensitive.
There is also zero chance of us introducing reassignment and making mutability first class in Elixir too. The reason for this is because we all agree that, the majority of the times, lack of reassignment and lack of mutability are features that make our code more readabily and understandable in the long term. The snippet above is one of the few examples where we are in the wrong end of the trade-offs.
Therefore, how can we move forward?
Comprehensions
Comprehensions in Elixir have always been a syntax sugar to more complex data-structure traversals. Do you want to have the cartesian product between all points in x
and y
? You could write this:
Enum.flat_map(x, fn i ->
Enum.map(y, fn j -> {i, j} end)
end)
Or with a comprehension:
for i <- x, j <- y, do: {i, j}
Or maybe you want to brute force your way into finding Pythagorean Triples?
Enum.flat_map(1..20, fn a ->
Enum.flat_map(1..20, fn b ->
1..20
|> Enum.filter(fn c -> a*a + b*b == c*c end)
|> Enum.map(fn c -> {a, b, c} end)
end)
end)
Or with a comprehension:
for a <- 1..20,
b <- 1..20,
c <- 1..20,
a*a + b*b == c*c,
do: {a, b, c}
There is no question the comprehensions are more concise and clearer, once you understand their basic syntax elements (which are, at this point, common to many languages).
However, a common point that arose during the discussion of how to solve this problem using comprehensions is that comprehensions are currently under utilized in Elixir. Therefore, I would like to introduce for let
and for reduce
in the form of a āGetting Startedā guide that could be hosted on the Elixir website.
The goal is to show how for
can be both a power-user tool and still useful to solve several problems in a format that developers may be familiar with, while still building an intuition on functional ideas.
A tour into for
While Elixir does not have loops as found in traditional languages, it does have a powerful for
construct, typical to many programming languages, where we can generate, filter, transform, and accumulate collections. In Elixir, we call it for-comprehension.
In this chapter, we will learn how to fully leverage the power behind for-comprehensions to perform many tasks similar to imperative languages, but in a functional manner.
If you want a fun challenge, try to rewrite all of the
for
uses below using theEnum
module. You can consider doing so in two variants: using a singleEnum
function and using a pipeline ofEnum
functions.
Generators
Letās start with a simple problem. You have a list of numbers and you want to multiply each element in the list by two. We can do this:
iex> for i <- [1, 2, 3] do
...> i * 2
...> end
[2, 4, 6]
The part i <- [1, 2, 3]
is a generator. It gets each value in the list [1, 2, 3]
and binds them to the variable i
one at a time. Once i
is bound, it executes the contents of the do-end
block. The new list is formed by the results of the do-end
block.
A comprehension can have multiple generators too. One use of multiple generators is to find all possible combinations between two lists. Imagine for example you are interested in a new car. You have identifier three colors that you like: green, blue, and yellow. You are also divided between three brands: Ford, Volkswagen, and Toyota. What are all combinations available?
Letās first define variables:
iex> colors = [:green, :blue, :yellow]
iex> cars = [:ford, :volkswagen, :toyota]
Now letās find the combinations:
iex> for color <- colors, car <- cars do
...> "#{color} #{car}"
...> end
["green ford", "green volkswagen", "green toyota", "blue ford",
"blue volkswagen", "blue toyota", "yellow ford", "yellow volkswagen",
"yellow toyota"]
By having two generators, we were able to combine all options into strings.
Multiple generators are also useful to extract all possible values that are nested within other colors. Imagine that you have a list of users and their favorite programming languages:
iex> users = [
...> %{
...> name: "John",
...> languages: ["JavaScript", "Elixir"]
...> },
...> %{
...> name: "Mary",
...> languages: ["Erlang", "Haskell", "Elixir"]
...> }
...> ]
If we want to get all languages from all users, we could use two generators. One to traverse all users and another to traverse all languages:
iex> for user <- users, language <- user.languages do
...> language
...> end
["JavaScript", "Elixir", "Erlang", "Haskell", "Elixir"]
The comprehension worked as if it retrieved the languages lists of all users and flattened it into a list, with no nesting.
The important concept about for-comprehensions so far is that we never use them to mutate values. Instead, we explicitly use them to explicitly map inputs to outputs: the lists that we want to traverse are given as inputs and for
returns a new list as output, based on the values returned by the do-end
block.
The :uniq
option
In the example above, you may be wondering: what if we want all languages from all users but with no duplicates? You are in lucky, comprehensions also accept options, one of them being :uniq
:
iex> for user <- users, language <- user.languages, uniq: true do
...> language
...> end
["JavaScript", "Elixir", "Erlang", "Haskell"]
Comprehension options are always given as the last argument of for
, just before the do
keyword.
Filters
So far we used comprehensions to map inputs to outputs, to generate combinations, or to flatten lists nested inside other lists. We can also use comprehensions to filter the input, keeping only the entries that match a certain condition. For example, imagine we have a list of positive and negative numbers, and we want to keep only the positive ones and then multiply them by two:
iex> for i <- [-5, -3, -2, 1, 2, 4, 8], i > 0 do
...> i * 2
...> end
[2, 4, 8, 16]
Filters are given as part of the comprehension arguments. If the filter returns a truthy value (anything except false
and nil
), the comprehension continues. Otherwise it skips to the next value.
You can give as many filters as you want, including mixed with other generators. Letās go back to our users example and add some arbitrary rules. Imagine that we only want to consider programming languages from users that have the letter āaā in their name:
iex> for user <- users, String.contains?(user.name, "a"), language <- user.languages do
...> language
...> end
["Erlang", "Haskell", "Elixir"]
As you can see, due to the filter, we skipped Johnās languages.
What if we want only the programming languages that start with the letter āEā?
iex> for user <- users, language <- user.languages, String.starts_with?(language, "E") do
...> language
...> end
["Elixir", "Erlang", "Elixir"]
Now we got languages from both, including the duplicates, but returned only the ones starting with āEā. You can still use the :uniq
option, give it a try!
Computing additional values with for let value = initial
So far, our comprehensions have always returned a single output. However, sometimes we want to traverse a collection and get multiple properties out of it too.
Letās go back to our initial example. Imagine that you want to traverse a list of numbers, multiple each element in it by two while returning the sum of the original list at the same time.
In most non-functional programming languages, you might achieve this task like this:
sum = 0
list = []
for(element of [1, 2, 3]) {
list.append(element * 2)
sum += element
}
list /* [2, 4, 6] */
sum /* 6 */
This is quite different from how we have been doing things so far. In the example above, the for
loop is changing the values of list
and sum
directly, which is then reflected in those variables once the loop is over.
However, we have already learned that comprehensions in Elixir explicitly receive all inputs and return all outputs. Therefore, the way to tackle this in Elixir is by explicitly declaring all additional variables we want to be looped and returned by the comprehension, using the for let
:
iex> for let sum = 0, i <- [1, 2, 3] do
...> sum = sum + i
...> {i * 2, sum}
...> end
{[2, 4, 6], 6}
Letās break it down.
Instead of starting with a generator, our comprehension starts with a let variable = initial
expression. let
introduces a new variable sum
, exclusive to the comprehension, and it starts with an initial value of 0. The same way that i
changes on every element of the list, sum
will have a new value on each iteration too.
Now that we have an additional variable as input to the comprehension, it must also be returned as output. Therefore, the comprehension do-end
block must return two elements: the new element of the list, as previously, and the new value for sum
. Those elements are returned in a tuple. Once completed, the comprehension also returns a two-element tuple, with the new list and the final sum as elements. In other words, the shape returned by for
matches the return of the do-end
block.
If you add IO.inspect/1
at the top of the do-end
block, you can see the values of i
and sum
as the comprehension traverses the collection:
iex> for let sum = 0, i <- [1, 2, 3] do
...> IO.inspect({i, sum})
...> sum = sum + i
...> {i * 2, sum}
...> end
And you will see this before the result:
{1, 0}
{2, 1}
{3, 3}
As you can see, both i
and sum
change throughout the comprehension.
Given the comprehension now returns a tuple, you can pattern match on it too. In fact, thatās most likely the pattern you will see in actual code, like this:
{doubled, sum} =
for let sum = 0, i <- [1, 2, 3] do
sum = sum + i
{i * 2, sum}
end
And then you can further transform the doubled
list and the sum
variable as necessary.
for let
augments for
to allow us to accumulate additional values. Albeit a bit more verbose than other languages, it is explicit: we can immediately look at it and see the inputs and outputs.
Accumulating multiple values
Sometimes you may need to accumulate multiple properties from a collection. Imagine we want to multiply each element in the list by two, while also getting its sum and count. To do so, we could give a tuple of variables to let
:
iex> for let {sum, count} = {0, 0}, i <- [1, 2, 3] do
...> sum = sum + i
...> count = count + 1
...> {i * 2, {sum, count}}
...> end
{[2, 4, 6], {6, 3}}
Once again, the shape we declare in let
(a two-element tuple) matches the shape we return from the do-block
and of the result returned by for
.
You could move the initialization of the let variables to before the comprehension:
iex> sum = 0
iex> count = 0
iex> for let {sum, count}, i <- [1, 2, 3] do
...> sum = sum + i
...> count = count + 1
...> {i * 2, {sum, count}}
...> end
{[2, 4, 6], {6, 3}}
let
can be a variable or a tuple of variables. If the variables are not initialized, it is expected for such variable to already exist, as in the example above.
Reducing a collection
We have learned how to use let
to traverse a collection and accumulate different properties from it at the same time. However, what happens when we are only interested in the properties and not in returning a new collection? In other words, how can we get only the sum
and count
out of a list, skipping the multiplication of each element by 2?
One option is to use let
and simply discard the list result:
{_doubled, {sum, count}} =
for let {sum, count} = {0, 0}, i <- [1, 2, 3] do
sum = sum + i
count = count + 1
{i, {sum, count}}
end
However, it seems wasteful to compute a new list, only to discard it! In such cases, you can convert the :let
into a :reduce
:
{sum, count} =
for reduce {sum, count} = {0, 0}, i <- [1, 2, 3] do
sum = sum + i
count = count + 1
{sum, count}
end
By using reduce
, we now only need to return the reduce
shape from the do-end
block, which once again is reflected in the result of the comprehension.
In other words, for reduce
is a special case of for let
, where we are not interested in returning a new collection. It is called reduce
precisely because we are reducing a collection into a set of accumulated values. Given that, you could consider let
to be a āmap and reduceā, as it maps inputs to outputs and reduces the collection into a set of accumulated values at the same time.
Summing up the guide
In this chapter we have learned the power behind Elixirās for-comprehensions and how it uses a functional approach, where we list our inputs and outputs, to mimic the power of imperative loops.
While we have used for-comprehensions to perform multiple tasks, such as computing the sum
and count
, in practice most developers would use the Enum
module to perform such trivial tasks. The Enum
module contains a series of recipes for the most common (and some also uncommon) operations. For example:
iex> Enum.map([1, 2, 3], fn i -> i * 2 end)
[2, 4, 6]
iex> Enum.sum([1, 2, 3])
6
iex> Enum.count([1, 2, 3])
3
Still, for-comprehensions can be useful for handling more complex scenarios.
Note we didnāt explore the full power of comprehensions either. We will discuss the additional features behind comprehensions whenever relevant in future chapters.
Conclusion
My hope is the guide above shows how for
can be both a power user tool but also useful in introducing a series of new idioms, unified by a single construct, without imposing all of the functional terminology (such as flatten
, map
, filter
, map_reduce
, etc) upfront. Those words are mentioned, but their introduction is casual, rather than the starting point.
With that said, we can now go back to the original problem and see how an Elixir solution could look like:
{sections, _acc} =
for let {section_counter, lesson_counter} = {1, 1}, section <- sections do
lesson_counter = if section["reset_lesson_position"], do: 1, else: lesson_counter
{lessons, lesson_counter} =
for let lesson_counter, lesson <- section["lessons"] do
{Map.put(lesson, "position", lesson_counter), lesson_counter + 1}
end
section =
section
|> Map.put("lessons", lessons)
|> Map.put("position", section_counter)
{section, {section_counter + 1, lesson_counter}}
end
Personally speaking, it considerably reduces the amount of noise, while still keeping clear what is the state/accumulator of each comprehension. If for let
and for reduce
are added, the existing :reduce
option will be deprecated.
There are still some implementation details that could be further explored, described in the sections below:
Syntax considerations
No new syntax is necessary to support the constructs above. Similar syntax has already been in use in projects like StreamData.
Similarly, one of the reasons we have for let
instead of let for
is because the second would require making let
a special form, which would definitely break existing code. Furthermore, let
is very specific to for
and it doesnāt apply to other constructs, so it makes more sense as a part of for
than otherwise.
Error messages
By declaring the shape we want to return in let
/reduce
, we can provide really good error messages. For example, imagine the user makes this error:
iex> for let {sum, count} = {0, 0}, i <- [1, 2, 3] do
...> sum = sum + i
...> count = count + 1
...> {i * 2, sum}
...> end
The error message could say:
** (ComprehensionError) expected do-end block to return {output, {sum, count}}, got: {2, 1}
Why let
/reduce
at the beginning?
One of the things we discovered as we explored this proposal is that, by declaring let
and reduce
at the beginning, it makes those constructs much more powerful. For example, we could implement a take
version of a collection easily:
for let count = 0, count < 5, x <- element do
{x, count + 1}
end
Or we could even have actual recursion:
for let acc = [:root], acc != [], x <- acc do
# Compute some notes and return new nodes to traverse
end
While we wonāt support these features in the initial implementation (a generator must immediately follow let
and reduce
), it shows much more can be achieved with the foundation outlined here.
Furthermore, the introduction of for let
and for reduce
opens up the possibility of new combinations in the future, such as for async
that is built on top of Task.async_stream/3
.