我正在尝试拨打struct
我使用的协议扩展方法制作的public struct AdjacentDifferenceArrayGenerator<T: Strideable where T.Stride == T>: GeneratorType {
private var array: [T]
public init<U: SequenceType where U.Generator.Element == T>(source: U) {
self.array = source.adjacentDifference()
}
public mutating func next() -> [T]? {
guard !array.isEmpty else {
return nil
}
defer {
self.array = self.array.adjacentDifference()
}
return self.array
}
}
public extension SequenceType where Generator.Element: Strideable {
public func adjacentDifference() -> [Self.Generator.Element.Stride] {
var result: [Self.Generator.Element.Stride] = []
var generator = AdjacentDifferenceGenerator(generator: self.generate())
while let difference = generator.next() {
result.append(difference)
}
return result
}
}
public extension SequenceType where Generator.Element: Strideable, Generator.Element.Stride: Strideable, Generator.Element.Stride.Stride == Generator.Element.Stride {
public func pyramidOfAdjacentDifferences() -> [[Self.Generator.Element.Stride]] {
var result: [[Self.Generator.Element.Stride]] = []
var array = self.adjacentDifference()
while !array.isEmpty {
result.append(array)
array = array.adjacentDifference()
}
//var generator = AdjacentDifferenceArrayGenerator(source: self)
//while let differences = generator.next() {
//result.append(differences)
//}
return result
}
:
AdjacentDifferenceArrayGenerator
在替代算法之后,在末尾附近评论的四行尝试使用与第一个扩展中的方法相同的算法。当我取消注释第一行时,游标等待“{{1}}”并说:“无法为类型'AdjacentDifferenceArrayGenerator&lt; _&gt;'调用初始化程序带有'(source:Self)'类型的参数列表“尽管它在代码中没有大写,但最后一个”Self“在错误消息中大写。在类型和初始化程序参数之间添加显式模板括号没有帮助。有什么想法吗?
答案 0 :(得分:0)
我通过取消第一次迭代来解决这个问题:
public extension SequenceType where Generator.Element: Strideable, Generator.Element.Stride: Strideable, Generator.Element.Stride.Stride == Generator.Element.Stride {
public func pyramidOfAdjacentDifferences() -> [[Self.Generator.Element.Stride]] {
var result = [self.adjacentDifference()]
if result.first!.isEmpty {
return []
}
var generator = AdjacentDifferenceArrayGenerator(source: result.first!)
while let differences = generator.next() {
result.append(differences)
}
return result
}
}
我必须测试第一次迭代结果是否合法,因为将其从循环中取出意味着我丢失了自动检查。我不知道是否使用“self
”是问题,或者是使用通用序列而不是确定数组是问题所在。这里还需要帮助....