skipWhile

skipWhile

accepts 1 input stream, a predicate function

  • It ignores all values emitted by the input stream as long as the predicate returns ✔ true
  • As soon as it returns ✔ false for one value:
    • this value is emitted
    • all further ones will be emitted whether they satisfy the predicate or not
  • If the input stream completes, the output stream completes at the same time

returns a new stream of filtered values

takeWhile

accepts 1 input stream, a 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

Published on Monday, 29 Jan. 2018
Revised on Monday, 8 Oct. 2018

Can you fill in the blanks? Try to find solutions that use only one card. If you are stuck, try at least to solve them with a chain of several cards. Watch the solutions below when you are ready.


Solutions

Predicates everywhere

Here is a function:

It returns ✔ true or ✘ false. This means you can use this function as a predicate on filtering cards. The six operations on this exercise are filtering operations. And all solutions are based on such a predicate.

Part I - Filter

Here is the most common filtering card:

This is how ❚ filter works with a predicate:

  • Each event of the input stream is given to the predicate
  • If the predicate returns ✔ true, the event can pass
  • Otherwise, the event is ignored
  • As a result, it returns a new stream of filtered events

Part II - Slice

Slicing is a sub-category of filtering. Cards from this sub-category take or skip everything while/until a certain condition is met. For example:

This is how ❚ takeWhile operates:

  • Each event value of the input stream is given to the predicate:
    • If the predicate returns ✔ true, the event can pass
    • Otherwise, the event is ignored and the output stream immediately completes, emitting a ◉ complete notification

❚ skipWhile is the counterpart of ❚ takeWhile. Note that, with ❚ skipWhile, the input stream and the output stream completes at the same time.

Note: ❚ take is another slicing card (it returns a new stream of at most N values). But you can’t use ❚ take(2) instead of ❚ takeWhile(predicate) to solve this exercise: watch my take vs takeWhile video to see the difference.

Part III - Pick

Finally, you can filter an input stream by emitting zero or one event value. For example, an output stream can emit only the first or the last event value of an input stream. To emit only the last value that satisfies a predicate, you can chain ❚ filter(predicate) and ❚ last().

In some reactive stream libraries (like RxJS), you can simply use ❚ first and ❚ last with an optional predicate:

This is how ❚ last operates with one input stream and a predicate:

  • When the input stream completes, the output stream:
    • emits the last value emitted by the input stream that satisfied the predicate
    • and immediately completes

Summary

See also


Pipeable operators - Build your own with RxJS!


The JavaScript pipeline operator proposal

Cédric Soulas Follow Hire me

Freelance Developer Advocate. Motion graphics with code. JavaScript and Elm. cedricsoulas.com

Subscribe to reactive.how newsletter

Join the Newsletter

Learn Reactive Programming and stay up-to-date:

Receive my latest news, product updates and programming visualizations. You can unsubscribe at any time.

Highlights

@CedricSoulas

Making an illustrated book!

The Illustrated Book of RxJS

Learn more →