The thing on the right of the leftward-facing arrow is known as a generator.
Here, our generator is a list.

We can use for comprehensions to combine items. For instance, let's describe a
deck of cards:

defmoduleForPlaygroundTestdo# ...test"generating a deck of cards"dosuits=[:clubs,:diamonds,:hearts,:spades]ranks=[2,3,4,5,6,7,8,9,10,:jack,:queen,:king,:ace]# I'll paste in the list of all cards, to compare againstall_cards=[{2,:clubs},{3,:clubs},{4,:clubs},{5,:clubs},{6,:clubs},{7,:clubs},{8,:clubs},{9,:clubs},{10,:clubs},{:jack,:clubs},{:queen,:clubs},{:king,:clubs},{:ace,:clubs},{2,:diamonds},{3,:diamonds},{4,:diamonds},{5,:diamonds},{6,:diamonds},{7,:diamonds},{8,:diamonds},{9,:diamonds},{10,:diamonds},{:jack,:diamonds},{:queen,:diamonds},{:king,:diamonds},{:ace,:diamonds},{2,:hearts},{3,:hearts},{4,:hearts},{5,:hearts},{6,:hearts},{7,:hearts},{8,:hearts},{9,:hearts},{10,:hearts},{:jack,:hearts},{:queen,:hearts},{:king,:hearts},{:ace,:hearts},{2,:spades},{3,:spades},{4,:spades},{5,:spades},{6,:spades},{7,:spades},{8,:spades},{9,:spades},{10,:spades},{:jack,:spades},{:queen,:spades},{:king,:spades},{:ace,:spades}]# Then we use a for comprehension to combine the suits and ranksresult=forsuit<-suits,rank<-ranks,do:{rank,suit}assertall_cards==resultendend

You an also add filters to your comprehensions, to restrict values from
appearing in the results. We'll use a range of integers as our generator,
keeping only the even ones:

test"filters in for comprehensions"doresult=fori<-0..10,rem(i,2)==0doiendassertresult==[0,2,4,6,8,10]end

So far, each of our comprehensions has produced a list. We can collect the
values into different structures as well. Let's collect them into a map instead
of a list:

test"collecting results into a map"doresult=# We'll use the `into` option to specify a different term to collect intofori<-0..2,into:%{}do# When collecting into maps, you're expected to return a 2-tuple with# the key and value.{"#{i}",i}endassertresult==%{"0"=>0,"1"=>1,"2"=>2}end

Summary

In today's episode we saw how to use comprehensions in Elixir, both for
enumerating simple collections as well as combining and filtering them. I hope
you have fun playing with enumerables using comprehensions. See you soon!

sign up for full access

Meet your expert

I've been building web-based software for businesses for over 18 years. In the last four years I realized that functional programming was in fact amazing, and have been pretty eager since then to help people build software better.