diff --git a/Sources/UB/Extensions/Array.swift b/Sources/UB/Extensions/Array.swift index dc88b8d..dbd5854 100644 --- a/Sources/UB/Extensions/Array.swift +++ b/Sources/UB/Extensions/Array.swift @@ -2,17 +2,8 @@ import Foundation extension Array where Element == Addr { func closest(to: Addr) -> Addr? { - var distance = 0 - var addr: Addr? - - forEach { peer in - let dist = peer.distance(to: to) - if distance > dist { - distance = dist - addr = peer - } + return self.min { a, b in + a.distance(to: to) < b.distance(to: to) } - - return addr } }