I'm trying to downsample a long collection by decimating or extracting every nth element.
Here's what I got for my array extension:
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
}
I'm expecting 50-100k items in the original array and will probably downsample to the native bounds width of the screen (500-1k points).
Is there a more concise or efficient way of doing this?