3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Enum.zip_reduce/4を楽しむ(Elixir)

Last updated at Posted at 2022-04-17

八重むぐら茂れるやどの寂しきに人こそ見えね秋は来にけり

Advent Calendar 2022 99日目1の記事です。
I'm looking forward to 12/25,2022 :santa::santa_tone1::santa_tone2::santa_tone3::santa_tone4::santa_tone5:
私のAdvent Calendar 2022 一覧


はじめに

Elixirを楽しんでいますか:bangbang::bangbang::bangbang:

この記事は、Enum.zip_reduce/4を楽しんでみます。

Enum.zip_reduce/4

Elixir 1.12 or laterで使えます。

Enum.zip/2Enum.reduce/3を理解していると、理解しやすいです。

公式の説明を転載します。

スクリーンショット 2022-04-17 16.31.33.png

Examples

例を2つほど紹介します。

例①

例①です。

iex> Enum.zip_reduce([1, 2], [3, 4], 0, fn x, y, acc -> x + y + acc end)
10

こんな具合に計算が進んでいます。

x = 1, y = 3, acc = 0 -> x + y + acc = 4
x = 2, y = 4, acc = 4 -> 2 + 4 + 4 = 10

例②

2つ目の例です。

iex> Enum.zip_reduce([1, 2], [3, 4], [], fn x, y, acc -> [x + y | acc] end)
[6, 4]

こんな具合に計算が進んでいます。

x = 1, y = 3, acc = [] -> [x + y | acc] -> [4 | []] -> [4]
x = 2, y = 4, acc = [4] -> [x + y | acc] -> [6 | [4]] -> [6, 4] 

Wrapping up :lgtm::lgtm::lgtm::lgtm::lgtm:

この記事は、Enum.zip_reduce/4をご紹介しました。

Enum.zip_reduce/3というものもあります。
この記事の内容をご理解いただいた方には楽勝だとおもいます。

Enjoy Elixir:bangbang::bangbang::bangbang:
$\huge{Enjoy\ Elixir🚀}$

以上です。


I organize autoracex.
And I take part in NervesJP, fukuoka.ex, EDI, tokyo.ex, Pelemay.
I hope someday you'll join us.

We Are The Alchemists, my friends!


  1. @kaizen_nagoya さんの「「@e99h2121 アドベントカレンダーではありますまいか Advent Calendar 2020」の改訂版ではありますまいか Advent Calendar 2022 1日目 Most Breakthrough Generator」から着想を得て、模倣いたしました。

3
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?