值的范围是ForwardIndexType
,所以你只能他们advance()
, 或计算distance()
,但是没有定义减法-
。提前金额必须是相应的 类型T.Distance
。因此,这将是一个可能的实现:
extension Range {
func splitEvery(nInEach: T.Distance) -> [Range] {
var result = [Range]() // Start with empty array
var from = self.startIndex
while from != self.endIndex {
// Advance position, but not beyond the end index:
let to = advance(from, nInEach, self.endIndex)
result.append(from ..< to)
// Continue with next interval:
from = to
}
return result
}
}
例:然而
println((0 ..< 10).splitEvery(3))
// Output: [0..<3, 3..<6, 6..<9, 9..<10]
注意0 ..< 10
不是整数的列表(或阵列)。要分割阵列成子阵列可以定义一个类似的延伸:
extension Array {
func splitEvery(nInEach: Int) -> [[T]] {
var result = [[T]]()
for from in stride(from: 0, to: self.count, by: nInEach) {
let to = advance(from, nInEach, self.count)
result.append(Array(self[from ..< to]))
}
return result
}
}
实施例:
println([1, 1, 2, 3, 5, 8, 13].splitEvery(3))
// Output: [[1, 1, 2], [3, 5, 8], [13]]
更一般的方法可以是分割所有可切片对象。但是Sliceable
是协议和协议不能被扩展。你可以做什么,而不是是 定义功能,是以可切片的对象作为第一个参数:
func splitEvery<S : Sliceable>(seq : S, nInEach : S.Index.Distance) -> [S.SubSlice] {
var result : [S.SubSlice] = []
var from = seq.startIndex
while from != seq.endIndex {
let to = advance(from, nInEach, seq.endIndex)
result.append(seq[from ..< to])
from = to
}
return result
}
(请注意,这功能是完全无关的(扩展)方法 定义上文)
例:
println(splitEvery("abcdefg", 2))
// Output: [ab, cd, ef, g]
println(splitEvery([3.1, 4.1, 5.9, 2.6, 5.3], 2))
// Output: [[3.1, 4.1], [5.9, 2.6], [5.3]]
范围一再没有可切片,但你可以定义一个单独的函数,它接受一个 范围的说法:
func splitEvery<T>(range : Range<T>, nInEach : T.Distance) -> [Range<T>] {
var result : [Range<T>] = []
var from = range.startIndex
while from != range.endIndex {
let to = advance(from, nInEach, range.endIndex)
result.append(from ..< to)
from = to
}
return result
}
例子:
println(splitEvery(0 ..< 10, 3))
// Output: [0..<3, 3..<6, 6..<9, 9..<10]
太棒了 - 一个通用的实现! – kfmfe04 2014-11-01 19:21:22
奇怪的是,这在Playground中完美地工作,但我有问题让它在一个项目中编译 - 我在这里发布了一个新条目:http://stackoverflow.com/q/26694296/975129 – kfmfe04 2014-11-01 23:20:35
@ kfmfe04:我有稍微扩大了答案。如果您需要更多信息,请与我们联系。 – 2014-11-02 09:58:27