Elixir-enumerables

提供:Dev Guides
移動先:案内検索

Elixir-列挙可能

列挙可能とは、列挙できるオブジェクトです。 「列挙」とは、セット/コレクション/カテゴリのメンバーを1つずつ(通常は順番に、通常は名前で)カウントオフすることを意味します。

Elixirは列挙型の概念と、列挙型と連携するためのhttps://elixir-lang.org/docs/stable/elixir/Enuml[Enum module]を提供します。 Enumモジュールの関数は、名前が示すように、データ構造内の値の列挙に制限されています。 列挙可能なデータ構造の例は、リスト、タプル、マップなどです。 Enumモジュールは、enumを処理する100を少し超える関数を提供します。 この章では、いくつかの重要な機能について説明します。

これらの関数はすべて、列挙可能な要素を最初の要素として、関数を2番目の要素として取り、それらを処理します。 以下に機能を説明します。

all?

*all* を使用する場合 関数では、コレクション全体がtrueに評価される必要があり、そうでない場合はfalseが返されます。 たとえば、リスト内のすべての要素が奇数であるかどうかを確認するには、次のようにします。
res = Enum.all?([1, 2, 3, 4], fn(s) -> rem(s,2) == 1 end)
IO.puts(res)

上記のプログラムが実行されると、次の結果が生成されます-

false

これは、このリストのすべての要素が奇数ではないためです。

any?

名前が示すように、この関数は、コレクションの要素がtrueと評価された場合にtrueを返します。 たとえば-

res = Enum.any?([1, 2, 3, 4], fn(s) -> rem(s,2) == 1 end)
IO.puts(res)

上記のプログラムが実行されると、次の結果が生成されます-

true

チャンク

この関数は、コレクションを2番目の引数として提供されるサイズの小さなチャンクに分割します。 たとえば-

res = Enum.chunk([1, 2, 3, 4, 5, 6], 2)
IO.puts(res)

上記のプログラムが実行されると、次の結果が生成されます-

[[each

It may be necessary to iterate over a collection without producing a new value, for this case we use the *each *function −

[source,prettyprint,notranslate]

Enum.each(["Hello"、 "Every"、 "one"]、fn(s)→ IO.puts(s)end)

When the above program is run, it produces the following result −

[source,result,notranslate]

みなさん、こんにちは

=== map

To apply our function to each item and produce a new collection we use the map function. It is one of the most useful constructs in functional programming as it is quite expressive and short. Let us consider an example to understand this. We will double the values stored in a list and store it in a new list* res *−

[source,prettyprint,notranslate]

res = Enum.map([2、5、3、6]、fn(a)→ a* 2 end)IO.puts(res)

When the above program is run, it produces the following result −

[source,result,notranslate]
=== reduce

The *reduce *function helps us reduce our enumerable to a single value. To do this, we supply an optional accumulator (5 in this example) to be passed into our function; if no accumulator is provided, the first value is used −

[source,prettyprint,notranslate]

res = Enum.reduce([1、2、3、4]、5、fn(x、accum)→ x + accum end)IO.puts(res)

When the above program is run, it produces the following result −

[source,result,notranslate]

15

The accumulator is the initial value passed to the* fn*. From the second call onwards the value returned from previous call is passed as accum. We can also use reduce without the accumulator −

[source,prettyprint,notranslate]

res = Enum.reduce([1、2、3、4]、fn(x、accum)→ x + accum end)IO.puts(res)

When the above program is run, it produces the following result −

[source,result,notranslate]

10

=== uniq

The uniq function removes duplicates from our collection and returns only the set of elements in the collection. For example −

[source,prettyprint,notranslate]

res = Enum.uniq([1、2、2、3、3、3、4、4、4、4])IO.puts(res)

When running above program, it produces the following result −

[source,result,notranslate]
=== Eager Evaluation

All the functions in the Enum module are eager. Many functions expect an enumerable and return a list back. This means that when performing multiple operations with Enum, each operation is going to generate an intermediate list until we reach the result. Let us consider the following example to understand this −

[source,prettyprint,notranslate]

odd? =&(奇数? =&(rem(&1、2)!= 0)res = 1..100_000 |> Enum.map(&(&1 *3))|> Enum.filter(odd?)|> Enum.sum IO.puts( res)

When the above program is run, it produces the following result −

[source,result,notranslate]

7500000000

The example above has a pipeline of operations. We start with a range and then multiply each element in the range by 3. This first operation will now create and return a list with 100_000 items. Then we keep all odd elements from the list, generating a new list, now with 50_000 items, and then we sum all entries.

The* |> *symbol used in the snippet above is the* pipe operator*: it simply takes the output from the expression on its left side and passes it as the first argument to the function call on its right side. It’s similar to the Unix | operator. Its purpose is to highlight the flow of data being transformed by a series of functions.

Without the *pipe *operator, the code looks complicated −

[source,prettyprint,notranslate]

Enum.sum(Enum.filter(Enum.map(1..100_000、&(&1* 3))、odd?))

We have many other functions, however, only a few important ones have been described here.