Closures
A closure is { params in body }. Trailing closures and $0 shorten code.
Example
let sq = { (x: Int) -> Int in x * x }
print(sq(5))
let n = [3, 1, 2]
print(n.map { $0 * 2 })
print(n.sorted { $0 > $1 })
print(n.filter { $0 > 1 })Exercise
Shorthand argument.
n.map { * 2 }
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.