kefirjs / kefir

A Reactive Programming library for JavaScript
https://kefirjs.github.io/kefir/
MIT License
1.87k stars 97 forks source link

takeNth? #161

Closed elsehow closed 8 years ago

elsehow commented 8 years ago

Hi!

I've written a small wrapper to take every nth value of a Kefir stream. I have no idea if my solution is eloquent, but would there be any interest in integrating this feature into Kefir?

rpominov commented 8 years ago

Hey!

Not sure, I've personally never needed this. I'd like to see what others think. What is your use case, btw? What do you need it for?

Btw, I'd implement it a bit simpler:

function takeNth (stream, n) {
  let count = 0
  return stream.filter(() => {
    count++
    return count % n === 0
  })
}

This method is inevitably stateful so it's ok to store some state in a closure variable.

elsehow commented 8 years ago

Nice solution! I've updated my package.

My use case is taking buffers of EEG data flowing over a serial port:

serialData.slidingWindow(512,512).takeNth(512).map(fft)
rpominov commented 8 years ago

I see. .slidingWindow(512,512).takeNth(512) probably could be replaced with .bufferWithCount(512) if we'd have bufferWithCount.

elsehow commented 8 years ago

Agreed. I'll try to work on those and maybe issue a PR. Thanks

rpominov commented 8 years ago

:+1: