通过在Swift中删除或提取每个nth元素的有效下样本收集方式



我正在尝试通过删除或提取每个nth元素来将长采样的收藏置于较长的集合中。

这是我为阵列扩展而得到的:

func downsampled(to threshold: Int) -> [T] {
    // Validate that threshold falls in valid range
    guard !isEmpty, 1...count ~= threshold else { return Array(self) }
    
    let skip = (count / threshold) + 1
    var index = 0
    
    var items = [T]()
    while index < count {
        items.append(self[index])
        index += skip
    }
    
    return items
}

我期望原始数组中的50-100k项目,可能会将样本下样本到屏幕的本机界限(500-1K点)。

是否有更简洁或有效的方法?

extension RangeReplaceableCollection {
    func every(from: Index? = nil, through: Index? = nil, nth: Int) -> Self { .init(stride(from: from, through: through, by: nth)) }
}

extension Collection {
    func stride(from: Index? = nil, through: Index? = nil, by: Int) -> AnySequence<Element> {
        var index = from ?? startIndex
        let endIndex = through ?? self.endIndex
        return AnySequence(AnyIterator {
            guard index < endIndex else { return nil }
            defer { index = self.index(index, offsetBy: by, limitedBy: endIndex) ?? endIndex }
            return self[index]
        })
    }
}

游乐场测试

let array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
for element in array.stride(by: 3) {
    print(element)
}
array.stride(by: 3).forEach {
    print($0)
}
let nth = array.every(nth: 3)  // [1, 4, 7, 10, 13]
let str = "0123456789"
for character in str.stride(by: 2) {
    print(character)
}
str.stride(by: 2).forEach {
    print($0)
}
let even = str.every(nth: 2)   // "02468"

最新更新