Closures with multiple operators in Swift
struct Point {
var x: Double
var y: Double
}
let getDistance = { (p1: Point, p2: Point) -> Double in
let d1 = pow(p1.x - p2.x, 2)
let d2 = pow(p1.y - p2.y, 2)
return sqrt(d1 + d2)
}
let point1 = Point(x: 0, y: 0)
let point2 = Point(x: 5, y: 5)
let distance = getDistance(point1, point2)
//distance is 7.071
- Log in to post comments