- Mastering Elixir
- André Albuquerque Daniel Caixinha
- 128字
- 2021-08-05 10:42:48
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 iteration–for 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
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.
- Getting Started with ResearchKit
- Power Up Your PowToon Studio Project
- Python爬蟲開發(fā):從入門到實(shí)戰(zhàn)(微課版)
- 編程珠璣(續(xù))
- 樂高機(jī)器人設(shè)計(jì)技巧:EV3結(jié)構(gòu)設(shè)計(jì)與編程指導(dǎo)
- 云計(jì)算通俗講義(第3版)
- 人人都是網(wǎng)站分析師:從分析師的視角理解網(wǎng)站和解讀數(shù)據(jù)
- PostgreSQL Replication(Second Edition)
- Building an RPG with Unity 2018
- 大模型RAG實(shí)戰(zhàn):RAG原理、應(yīng)用與系統(tǒng)構(gòu)建
- Kotlin從基礎(chǔ)到實(shí)戰(zhàn)
- RabbitMQ Essentials
- Visual C#.NET Web應(yīng)用程序設(shè)計(jì)
- Getting Started with Python and Raspberry Pi
- 代替VBA!用Python輕松實(shí)現(xiàn)Excel編程