I have an async data stream that sends more data than I want to process. Is there an efficient way to toss 9 of 10 elements?
If you really only want to keep one in every ten elements, you could do this trivially with AsyncAlgorithms' chunks(ofCount:)
method:
asyncSequence.chunks(ofCount: 10)
.compactMap(\.first)
But why do you want to do this? If the issue is data rate, something like their throttle(...)
methods might be more applicable. And if your stream isn't an AsyncSequence
then you'll have to either convert it to one or use a different approach entirely.
1 Like
This would be perfect but I'm trying to use the beta CoreHID to get data from an HID device. CoreHID function monitorNotifications returns an AsyncThrowingStream. It conforms to AsyncSequence, so I should be able to use AsyncAlgorithms? I'll check it out.
Thanks for responding!
Throttle works! Thanks.
One question:
Why is it showing up as _throttle, not throttle?