Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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

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 如何扩展阵列<;双倍>;?_Arrays_Swift_Extension Methods - Fatal编程技术网

Arrays 如何扩展阵列<;双倍>;?

Arrays 如何扩展阵列<;双倍>;?,arrays,swift,extension-methods,Arrays,Swift,Extension Methods,使用元素类型的Double扩展数组的语法是什么 我已经看到了这样的答案: extension Sequence where Iterator.Element == Double { public func multiply(by factor: Double) -> [Double] { return self.map { $0 * factor } } } 但它扩展了一个通用的序列,因此既不允许通过索引进行随机访问,也不允许对计数属性进行随机访问。因此,

使用
元素
类型的
Double
扩展
数组
的语法是什么

我已经看到了这样的答案:

extension Sequence where Iterator.Element == Double {
    public func multiply(by factor: Double) -> [Double] {
        return self.map { $0 * factor }
    }
}
但它扩展了一个通用的
序列
,因此既不允许通过索引进行随机访问,也不允许对
计数
属性进行随机访问。因此,例如,我无法实现以下内容:

public func windowingFunc(index: Int, N: Int) -> Double {
    // ...
}

extension Sequence where Iterator.Element == Double {
    public func applyWindowing() -> [Double] {
        return (0..<self.count).map{self[$0] * windowingFunc(index: $0, N: self.count)}
    }
}
public func windowingFunc(索引:Int,N:Int)->Double{
// ...
}
迭代器.Element==Double的扩展序列{
公共函数applywing()->[Double]{

return(0..我通过添加大量约束来实现这一点:

extension RandomAccessCollection where Iterator.Element == Double, IndexDistance == Int, Index == Int {
    public func applyWindowing() -> [Double] {
        return (0..<self.count).map{self[$0] * windowingFunc(index: $0, N: self.count)}
    }
}
extension RandomAccessCollection,其中Iterator.Element==Double,IndexDistance==Int,Index==Int{
公共函数applywing()->[Double]{

return(0..如果要映射数组元素并需要其索引位置,则应使用方法
enumerated()
。我还将扩展
BidirectionalCollection
而不是
RandomAccessCollection
,正如@Hamish使用enumerated的评论中所述,您可以省略
索引==Int
约束

protocol BidirectionalCollection : BidirectionalIndexable, Collection
描述:支持向后和向前的集合 遍历。双向集合提供从任何位置向后遍历 有效索引,不包括集合的startIndex 因此,集合可以提供额外的操作,例如last 属性,该属性提供对最后一个元素和 按相反顺序显示元素的reversed()方法 此外,双向集合具有更高的效率 一些序列和收集方法的实现,例如 后缀(\:)


在Swift 3.1(Xcode 8.3 beta版中提供)中,您可以说
扩展数组,其中Element==Double
:)相关:听起来很简单!在我最新发布的Xcode上不起作用:(关于这一点,Xcode在哪里提供了它支持的Swift版本的信息?您目前可以从苹果的开发者页面下载Xcode 8.3测试版–您也可以运行
xcrun Swift-version
,找到您的Swift版本。@Alexander它不会过时,因为它将被弃用,但Swift 3.1将允许具体的constrained extensions,简化版(给定OPs特定需求)可以利用这一新功能,简单地将
数组
扩展到具体的
元素
类型(即
双重
)。@Alexander重复:“给定OPs特定需求”,即OP的明确问题,“扩展元素类型为Double的数组的语法是什么?”(甚至是问题标题)那么新功能正好允许这种方法。当然,关于良好实践的建议(以及为什么,例如切片)总是好的,但是考虑到问题评论中的讨论,我发现OPs对这个答案的评论自己并不感到困惑,并且我自己(在OP的一部分)给你提供了一个评论来解释这一点。或者这个问题不是一个质疑性的问题……?:)…在另一点上,我通常认为非问题是作为问题提出的(这里并不是说这种情况)与其分享有启发性的见解本身是毫无建设性的,因为这将导致浪费时间的混乱评论讨论,而这些讨论本来可以留下有用的有见解的良好实践评论。@Danra
return enumerated().map{$0.element*windowingFunc(index:$0.offset,N:count)}
@LeoDabus太好了!请将此作为答案发布给其他读者。谢谢!
extension BidirectionalCollection where Iterator.Element == Double, IndexDistance == Int {
    public func applyWindowing() -> [Iterator.Element] {
        return enumerated().map{ $0.element * windowingFunc(index: $0.offset, N: count)}
    }
}