Skip to content

Instantly share code, notes, and snippets.

@masakid
Created July 5, 2015 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masakid/fe860b7e06711985e84d to your computer and use it in GitHub Desktop.
Save masakid/fe860b7e06711985e84d to your computer and use it in GitHub Desktop.
[swift]配列から2で割り切れるものの合計
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