官术网_书友最值得收藏!

Comprehensions

Elixir provides another construct to iterate collections: comprehensions. As with the functions from the Enum module, comprehensions work on anything that implements the Enumerable protocol. Let's see a simple example:

iex> for x <- [2, 4, 6], do: x * 2
[4, 8, 12]

While, in this simple example, it is similar to Enum.map/2, comprehensions bring some other interesting features. You can, for instance, iterate over multiple collections and also apply filters. Let's see these two being applied in the following example:

iex> for x <- [1, 2, 3], y <- [4, 5, 6], Integer.is_odd(x), do: x * y
[4, 5, 6, 12, 15, 18]

Here we're doing a nested iterationfor each element of the first enumerable (which is represented by x), we will iterate through all elements of the second enumerable (represented by y). Also, we're applying a filter, and the body of our comprehension only gets executed when x is odd.

We won't be using comprehensions in the application we'll build throughout this book. However, it's important to mention them, as there are cases where using a comprehension instead of functions from the Enum module renders more elegant and expressive code

In our example, all comprehensions are returning a list, which is the default behavior. We can change that by passing the into: option, as you can see in this example:

iex> for x <- [1, 2, 3], into: %{}, do: {x, x + 1}
%{1 => 2, 2 => 3, 3 => 4}

As you can see, now we're getting a map back. The into: option takes a collection that will receive the results of the comprehension. This collection must implement the Collectable protocol. This protocol can be seen as the opposite of the Enumerable protocol, and is used to create a new structure from the values of an existing collection. This also has usage outside of comprehensions the  Enum.into/2 function uses this protocol to create a new collection based on an enumerable.
主站蜘蛛池模板: 灌云县| 谷城县| 高雄市| 富顺县| 顺平县| 凌源市| 临西县| 苏尼特右旗| 磐安县| 青龙| 峨山| 永川市| 盐源县| 富源县| 广州市| 济南市| 武安市| 盐津县| 洮南市| 静宁县| 老河口市| 睢宁县| 仁怀市| 城市| 梧州市| 右玉县| 永顺县| 乌兰浩特市| 沁阳市| 平罗县| 文登市| 宁蒗| 怀集县| 丁青县| 鲁甸县| 安仁县| 清镇市| 灵宝市| 嘉禾县| 青州市| 武穴市|