Skip to content

Instantly share code, notes, and snippets.

@takasek
Created March 29, 2016 05:27
Show Gist options
  • Save takasek/c364c9c12cf1910f4911 to your computer and use it in GitHub Desktop.
Save takasek/c364c9c12cf1910f4911 to your computer and use it in GitHub Desktop.
高階関数で植木算、どうしたらエレガントに書けるかな。[10,10,10]にマージン1を足して32を出力したい。enumerateしてindexを見て分岐? #CodePiece
let a: Int = [10,10,10]
.enumerate()
.reduce(0) { (sum: Int, item: (index: Int, value: Int)) in
sum + item.value + (item.index == 0 ? 0 : 1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment