Skip to content

Instantly share code, notes, and snippets.

@hachinobu
Last active August 29, 2015 14:20
Show Gist options
  • Save hachinobu/b09262a49043914339e6 to your computer and use it in GitHub Desktop.
Save hachinobu/b09262a49043914339e6 to your computer and use it in GitHub Desktop.
Swiftのreduce便利

var evens = [Int]()
for i in 1...10 {
if i % 2 == 0 {
evens.append(i)
}
}
var evenSum = 0
for i in evens {
evenSum += i
}
println(evenSum)


evenSum = Array(1...10)
.filter { (number) in number % 2 == 0 }
.reduce(0) { (total, number) in total + number }
println(evenSum)

ともに30が返る

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment