gpt4 book ai didi

arrays - 通过抽取或提取 Swift 中的每个第 n 个元素进行下采样收集的有效方法

转载 作者:可可西里 更新时间:2023-11-01 00:36:02 25 4
gpt4 key购买 nike

我正在尝试通过抽取或提取每个第 n 个元素来对长集合进行下采样。<​​/p>

这是我的数组扩展:

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 个项目,并且可能会缩减采样到屏幕的 native 边界宽度(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]
})
}
}

Playground 测试

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"

关于arrays - 通过抽取或提取 Swift 中的每个第 n 个元素进行下采样收集的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41029912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com