What is an Elixir Comprehension?
It is another way to loop over an Enumerable, just like the Enum module, only with a cleaner syntax.
What Elixir data types are Enumerable?
List, Map, Range, MapSet
How does an Elixir Comprehension differ from an Enum?
There is no difference in terms of performance and what you can accomplish functionally. The difference is in developer preference.
What is a the generator in a Comprehension?
It is the Enumerable being passed into the comprehension. In the below example `<- [1, 3, 5]` is the generator being passed into the comprehension.
```
for n <- [1, 3, 5], do: n * 2
```
Can multiple generator's be used within a Comprehension?
Yes. If you need to perform a set of operations on an Enumerable, you can use multiple generators.
Can you use pattern matching with Comprehensions?
Yes. The below example pattern matches a keyword list of http responses for user requests and returns only the successes.
```
responses = [ok: %{name: "Joe", email: "joe@friendo.com"}, error: "invalid request", error: "invalid request", ok: %{name: "Jen", email: "jen@friendo.com"}]
for {:ok, msg} <- responses, do: msg
=> [
%{email: "joe@friendo.com", name: "Joe"},
%{email: "jen@friendo.com", name: "Jen"}
]
```
Write the equivalent Comprehension and Enum code for accepting a List and multiplying it by two.
```
for n <- [1, 3, 5], do: n * 2
```
Enum.map([1,3,5], &(&1 * 2))