Skip to content

Instantly share code, notes, and snippets.

let nums = [1,2,3,4,5,6,7,8,9,10]
let total = nums.reduce(1, *)
print(total) // 3628800になる
let nums = [1,2,3,4,5,6,7,8,9,10]
let total = nums.reduce(0, +)
print(total) // 55になる
let add: (Int, Int) -> Int = (+)
let nums = [1,2,3,4,5,6,7,8,9,10]
let calculator: (Int, Int) -> Int = { currentTotal, num in
let newTotal = currentTotal + num
return newTotal
}
//let add: (Int, Int) -> Int = (+)
let total = nums.reduce(0, calculator)
let nums = [1,2,3,4,5,6,7,8,9,10]
let total
= nums.reduce(0, { currentTotal, num in
let newTotal = currentTotal + num
return newTotal
})
print(total) // 55になる
let 配列 = [1,2,3,4,5,6,7,8,9,10]
let パックマンの初期値 = 0
let 最終的なパックマンの値
= 配列.reduce(パックマンの初期値, { パックマンの現在値, 配列から1個取り出した値 in
// パックマンに配列の要素を1つ食わせるたびに以下の処理が実行される
let 新たなパックマンの値 = パックマンの現在値 + 配列から1個取り出した値
return 新たなパックマンの値
})
let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let total = nums.reduce(0, +)
print(total)
let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var total = 0
for num in nums {
total += num
}
print(total)
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let indexPath = tableView.indexPathForSelectedRow {
tableView.deselectRow(at: indexPath, animated: true)
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
performSegue(withIdentifier: "ShowList2", sender: nil)
}