Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Swift中Int数组的样本变量_Arrays_Swift_Map Function - Fatal编程技术网

Arrays Swift中Int数组的样本变量

Arrays Swift中Int数组的样本变量,arrays,swift,map-function,Arrays,Swift,Map Function,在此基础上,我试图计算Int数组的方差 到目前为止,我的扩展如下所示: extension Array where Element: Integer { /// Returns the sum of all elements in the array var total: Element { return reduce(0, combine: +) } /// Returns the average of all elements in the a

在此基础上,我试图计算Int数组的方差

到目前为止,我的扩展如下所示:

extension Array where Element: Integer {
    /// Returns the sum of all elements in the array
    var total: Element {
        return reduce(0, combine: +)
    }
    /// Returns the average of all elements in the array
    var average: Double {
        return isEmpty ? 0 : Double(total.hashValue) / Double(count)
    }

    /// Returns an array of the squared deviations from the mean
    var squaredDeviations: [Double] {
        let mean = average
        return isEmpty ? 0 : map( { number in
            let difference = Double(number) - mean
            return pow(distance, 2)
        })
    }
}

Total和average工作正常,但对于squaredDifferences,在使用
map
时,似乎必须返回与数组相同的类型。有没有办法绕过这个问题

更新我收到编译器错误:

“?”中的结果值:表达式具有不匹配的类型“Int”和 “[[uuz]”


问题是我返回的0不是一个double数组。我也没有使用number.hashValue,因此无法初始化double。

这里的问题是,可以返回两个可能的值:

return isEmpty ? 0 : map( { number in
    let difference = Double(number) - mean
    return pow(distance, 2)
})
让我们将这些
条件操作符分解为
if
/
else

if isEmpty {
    return 0 //inferred return type: Int
}
else {
    return map( { number in
        let difference = Double(number) - mean
        return pow(distance, 2)
    }) // inferred return type: [Double]
}
这两种可能的回报有不同的类型

方差是与平均值之差的平方的平均值。您忘记执行平均步骤。请尝试:

var squaredDeviations: Double {
    let mean = average
    return isEmpty ? 0.0 : self.map{ pow(Double($0) - mean, 2) }.average
}
另一方面:我建议不要使用计算属性进行昂贵的计算。它提供了一个误导性的API,没有明确说明这是一个缓慢的线性时间过程。我将这样做:

extension Array where Element: Integer {
    /// Returns the sum of all elements in the array
    func summed() -> Element {
        return self.reduce(0, combine: +)
    }

    /// Returns the average of all elements in the array
    func averaged() -> Double {
        return Double(self.summed()) / Double(count)
    }

    /// Returns an array of the squared deviations from the mean
    func squaredDeviations() -> [Double] {
        let average = self.averaged()
        return self.map { pow(Double($0) - average, 2) }
    }

    /// Returns the variance of the Array
    func variance() -> Double {
        return self.squaredDeviations().averaged()
    }
}

t似乎在使用map时必须返回与数组相同的类型
Not true,map的存在主要是为了方便类型之间的转换。”似乎在使用map时必须返回与数组相同的类型“不,没有这样的规定。您可以从
map
函数返回任何类型。事实上,你是这样做的:一个整数进来,一个双精度出来。这是你的真实代码吗???问题到底是什么?问题是,如果数组为空,则尝试返回
0
,这不是一个双精度数组。答案就在那里(参考文献AMomchilov),我有另一个注意事项。小心,不要在平均计算中混用value和hashValue!无法保证,Int值与hashValue相同……您建议我改用toIntMax()吗?谢谢。我打算把方差作为一个单独的变量来分解,因为我希望能够得到平均值的平方偏差。哦,我明白了。我会把它做成真正的quickSee我的编辑。我会为了昂贵的计算避免计算属性。假设
averaged()
是一个计算属性而不是一个函数。作为API的消费者,它不是很明显的线性时间。假设我编写了
map{pow(Double($0)-self.average,2)}
而不是使用
let average=self.averaged()
缓存变量。在不知情的情况下,
squaredeviations()
函数的时间复杂度从线性变为二次。@TimVermeulen例如,
String.lowercasesetring
已更改为
Swift.lowercased()