Skip to content

Instantly share code, notes, and snippets.

@kmizu
Created March 25, 2018 03:03
Show Gist options
  • Save kmizu/ba2ad6d73f33d532d1a8d0da71fdcb7c to your computer and use it in GitHub Desktop.
Save kmizu/ba2ad6d73f33d532d1a8d0da71fdcb7c to your computer and use it in GitHub Desktop.
fold / reduce のススメ(あるいは高階関数のススメ) ref: https://qiita.com/kmizu/items/c951d5d50c351f39d46c
array.reduce((sum, x) => sum + x, 0) // 0はあえて明示した
list.foldLeft(0){(sum, x) => sum + x}
array.fold(0, |sum, x| sum + x)
array.inject(0){|sum, x| sum + x}
var sum = 0
array.forEach((x) => {
sum += x
})
var sum = 0
for(var i = 0; i < array.length; i++) {
sum += array[i]
}
array.reduce((sum, x) => sum + x, 0)
9
/ \
4 5
/ \
1 3
/ \
0 1
array.sum()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment