您应该使用reduce
方法对序列元素求和,如下所示:
Xcode Xcode 10.2+ • Swift 5 or later
extension Sequence where Element: AdditiveArithmetic {
/// Returns the total sum of all elements in the sequence
func sum() -> Element { reduce(.zero, +) }
}
extension Collection where Element: BinaryInteger {
/// Returns the average of all elements in the array
func average() -> Element { isEmpty ? .zero : sum() / Element(count) }
/// Returns the average of all elements in the array as Floating Point type
func average<T: FloatingPoint>() -> T { isEmpty ? .zero : T(sum()) / T(count) }
}
extension Collection where Element: BinaryFloatingPoint {
/// Returns the average of all elements in the array
func average() -> Element { isEmpty ? .zero : sum() / Element(count) }
}
let votes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let votesTotal = votes.sum() // 55
let votesAverage = votes.average() // 5
let votesDoubleAverage: Double = votes.average() // 5.5
如果您需要处理Decimal
种类型,那么AdditiveArithmetic
协议扩展方法已经涵盖了它的总和,因此您只需要实现平均值:
extension Collection where Element == Decimal {
func average() -> Decimal { isEmpty ? .zero : sum() / Decimal(count) }
}
如果需要对自定义 struct 的某个属性求和,我们可以扩展Sequence
并创建一个方法,该方法将KeyPath作为参数来计算其和:
extension Sequence {
func sum<T: AdditiveArithmetic>(_ predicate: (Element) -> T) -> T {
reduce(.zero) { $0 + predicate($1) }
}
}
用法:
struct User {
let name: String
let age: Int
}
let users: [User] = [
.init(name: "Steve", age: 45),
.init(name: "Tim", age: 50)]
let ageSum = users.sum(\.age) // 95
并扩展集合以计算其平均值:
extension Collection {
func average<T: BinaryInteger>(_ predicate: (Element) -> T) -> T {
sum(predicate) / T(count)
}
func average<T: BinaryInteger, F: BinaryFloatingPoint>(_ predicate: (Element) -> T) -> F {
F(sum(predicate)) / F(count)
}
func average<T: BinaryFloatingPoint>(_ predicate: (Element) -> T) -> T {
sum(predicate) / T(count)
}
func average(_ predicate: (Element) -> Decimal) -> Decimal {
sum(predicate) / Decimal(count)
}
}
用法:
let ageAvg = users.average(\.age) // 47
let ageAvgDouble: Double = users.average(\.age) // 47.5