Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: take items from stream while value is less than a max value #378

Closed
bapti opened this issue Oct 3, 2015 · 7 comments
Closed

Comments

@bapti
Copy link

bapti commented Oct 3, 2015

Hi, I've got a quick quesiton about how to take items from an infinite stream - I'd like to be able to do something like below.

_(infiniteNumberGenerator)
  .takeWhile( (count) => {
    return count < max;
  })
  .map(...)

Is this supported with the methods or would I have to implement something? Take does this by using slice. I could estimate how many i need using some maths but I thought it would be nice just to have a clause that stops the stream after enough has been pulled. Is this doable?

Thanks
Neil

@bapti bapti changed the title Question: take while count is less than Question: take items from stream while value is less than a max value Oct 3, 2015
@bapti
Copy link
Author

bapti commented Oct 3, 2015

Sorry, figured it out

var takeWhile = function (f) {
  return (err, x, push, next) => {
    if (err) {
      // pass errors along the stream and consume next value
      push(err);
      next();
    }
    else if (x === _.nil) {
      // pass nil (end event) along the stream
      push(null, x);
    }
    else {
      // pass on the value only if the value passes the predicate
      if (f(x)) {
        push(null, x);
        next();
      } else {
        push(null, _.nil)
      }
    }
  }
}

paired with

.consume(takeWhile((x) => {
      return x < max
    }))

@bapti bapti closed this as completed Oct 3, 2015
@LewisJEllis
Copy link
Collaborator

Glad you worked this out, and with pure highland you're spot on what you're looking for. One note to be aware of - transducer support was added in #223 and takeWhile is available as a transducer (see also https://github.com/cognitect-labs/transducers-js), so you can use that to avoid writing your own takeWhile implementation if you so prefer.

@bapti
Copy link
Author

bapti commented Oct 11, 2015

@LewisJEllis - is this something that you'd like as a pull request for highland? Happy to add it with tests.

@LewisJEllis
Copy link
Collaborator

Probably not now, but almost definitely after 3.0 and the module system. We try to keep the core lib from becoming a 'kitchen sink', but takeWhile could easily fit in a module after 3.0. For now, we access the kitchen sink via transducers.

@bapti
Copy link
Author

bapti commented Oct 17, 2015

@LewisJEllis thanks for that, is there a ballpark for the 3.0 release?

@apaleslimghost
Copy link
Collaborator

Soon™ 😉

Not committing to anything here, but possibly some time in the next 6 months? We're tracking progress over at #179, although we've not made much progress for the last couple of months.

@mauriciocap
Copy link
Contributor

PR with takeWhile #677 , thanks!

vqvu added a commit that referenced this issue Mar 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants