9
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 1 year has passed since last update.

ElixirAdvent Calendar 2022

Day 16

Elixirから覚えるJavaScript 〜reduce関数〜

Last updated at Posted at 2023-01-03

僕は普段はElixirを使ってます
必要になったのでJavaScriptの勉強をはじめました
題名はElixirから覚えるJavaScriptですが、逆もできるかも?

今日はreduceを見てみたいと思います

お題
[1, 2, 3, 4, 5]の合計をする

Elixir

※Enum.sumはあえて使いません

x = [1, 2, 3, 4, 5]
y = Enum.reduce(x, 0, fn x, acc -> acc + x end)
IO.inspect(x)
IO.inspect(y)

実行結果
[1, 2, 3, 4, 5
15

JavaScript

x = [1, 2, 3, 4, 5];
y = x.reduce((acc, x) => acc + x, 0);
console.log(x);
console.log(y);
実行結果
[ 1, 2, 3, 4, 5 ]
15

無事に同じ結果になりました

9
1
0

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
9
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?