take

1 input stream amount

  • When the input stream emits its n-th value
    • if n < amount, the value is emitted
    • if n == amount, the value is emitted and the stream immediately completes

returns a new stream of at most amount values

takeWhile

1 input stream predicate function

  • When the input stream emits a value, it is given to the predicate
    • If the predicate returns ✔ true, the value is emitted
    • otherwise, the value is ignored and the stream immediately completes

returns a new stream of filtered values

Exercise

In the animation above, can you spot the difference between the outputs? Can you explain why?

Answer: the ◉ complete notification happens at a different moment:

  • On the one hand, ❚ take returns a stream that emits at most amount values and can complete as soon as this amount is reached.
  • On the other hand, ❚ takeWhile has to wait for the predicate to return ✘ false in order to complete.

See also


take(1) vs first()


takeLast vs take


Can you fill in the blanks?


Project vs Predicate functions

💌 Learn Reactive Programming and stay tuned

Occasional updates, plus:

News

@CedricSoulas