首页 > 解决方案 > Swift 数字和单型集合

问题描述

交换伙伴。最近,我通过解决一些琐碎的练习来熟悉自己,swift并遇到了一个应该计算统计数据的问题。目标是计算一个数字数组的平均值(所有元素的总和除以数量)。我决定采用最知名的方法来解决这个问题,并使用返回数值的函数定义了一个集合的扩展:

//this is specific case with Integers. Seems bold enought
extension Array where Element == Int {
    func mean() -> Double {
        var result = 0.0
        for i in self {
            result += Double(i)
        }
        return result / Double(self.count)
    }
}

[1,2,3,4,5,6,7,8,9].mean() //outputs 5.0

上面的代码运行没有问题。但我希望能够应用于mean()任何数字序列(浮点数、双精度数等),所以尝试是:

extension Collection where Element: Numeric {
    func mean() -> some Numeric {
        var result = Element.init(exactly: 0)!
        for i in self {
            result += i
        }
        return result / Element.init(exactly: self.count)! 
    }
}

这将允许从任何数字数组中获得结果:

var array1: [Float] = ...
var array2: [UInt] = ... 
var array3: [Int8] = ... //nutty case

swift但是编译失败,并显示一条消息,因为该集合中的值可以是不同的具体类型,由于严格的类型,不可能调用除法运算符。因此,实现所需功能的唯一选择是扩展所有使用数字操作的具体类型(Int、UInt、UInt8、...),这是一件很稳健的事情(大约有 20 个)。鉴于swift的扩展具有很大的抽象化可能性,是否有可能:

extension Collection where Adopter: HomogeneousCollection { ... }

此外,有些类型不能是大数:Int8 最多可以容纳 256 个数字,因此[Int8]如果元素少于 128 个,集合 of 只能给出正确的结果,否则nil会发生除以。

所以我问,声明一个函数来计算同质顺序集合的平均值的最合适的方法是什么?另外,当集合为空时,您将如何解决?

标签: swiftnumbersprotocols

解决方案


我找到了一个合适的解决方案。

extension Collection where Element: Numeric {
    /// Returns the total sum of all elements in the array
    var total: Element { return reduce(0, +) }
}

extension Collection where Element: BinaryInteger {
    /// Returns the average of all elements in the array
    var average: Double? {
        return isEmpty ? nil : Double(total) / Double(count)
    }
}

extension Collection where Element: BinaryFloatingPoint {
    /// Returns the average of all elements in the array
    var average: Element? {
        return isEmpty ? nil : total / Element(count)
    }
}

(1...9).average //Range<Int> -> 5.0

[1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9].average //[Double] -> 5.5

虽然我希望结果与它所属的集合具有相同的类型,但[Type] -> Type实际上这并不总是必要的,Double 对我来说就足够了[Type] -> Double。事实证明,在这种情况下,出于某种原因,swift 强制集合是同质的。起初我担心上面的代码不起作用,因为集合(数组)被初始化为保存不透明类型,但编译报告了一个消息:'BinaryInteger' 只能用作通用约束,因此这里保留了具体类型.

//this compiles
protocol SomeProtocol {}
struct SomeStruct: SomeProtocol {}
struct AnotherStruct: SomeProtocol {}

var myObject: [SomeProtocol] = [SomeStruct(), AnotherStruct()]

//but this doesnt, though all elements conform to BinaryInteger
let arr: [BinaryInteger] = [0 as Int, 1 as Int8, 2 as Int16]

推荐阅读