From 61bdfe6062a847d1d33f520bd3645c2bb672ccad Mon Sep 17 00:00:00 2001 From: decanus <7621705+decanus@users.noreply.github.com> Date: Mon, 18 May 2020 13:34:08 +0200 Subject: [PATCH] using min --- Sources/UB/Extensions/Array.swift | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) 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 } }