Created
July 5, 2015 16:17
-
-
Save masakid/fe860b7e06711985e84d to your computer and use it in GitHub Desktop.
[swift]配列から2で割り切れるものの合計
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let array = [1,2,3,4,5,6,7,8,9,10] | |
//filter(){$0 % 2 == 0}. : 配列から2で割り切れるものを抽出 | |
//educe(0){$0+$1} : 初期値を0として、抽出値を全て足す | |
let nums = array.filter(){$0 % 2 == 0}.reduce(0){$0 + $1} | |
println(nums) | |
// -> 30 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment