Reading through the book “Learn you some Erlang for Great Good” and learned a bit more about List Comprehensions than I knew before. If you are new to the world of Erlang, you’ll soon discover these marvels. Here is some of what I know about them…
First, what exactly is a list comprehension? It’s a way to map and filter through List(s). Here is a small example of mapping, it multiples all values in the list by two.
X matches every element in the list on the right hand side of the
the values are collected into a new list with the left-hand of
matching that happens here will ignore any mis-matches, as in the following
example where non-fruit is filtered out of a list of tuples.
This is just one way to filter down items. Another is to provide any number of clauses after the match, such as the following.
These filter expressions must evaluate to either
false. They give
you a more power over what is filtered when simple matching won’t work by
You can have any number of lists, as long as you have at least one!
This creates a Cartesian product between the lists, which can be handy at times when you want to intersect lists perhaps.