Operators and functions - Part 1

In Reactive Programming, operators take one or several input streams and return a new stream. Some operators may accept additional arguments, such as functions:

  • project function,
  • predicate function,
  • accumulator function, etc.

These functions don’t have to work with the input or the output stream: they usually just transform, compare or combine values. The operators do work with the streams, in conjunction with these functions.

Project and predicate

How you label a function (eg. “project” or “predicate” function) depends on how you use it.

A function that:

  • takes several values
  • and returns one new value of any type

may be used as a project function on combining operators (eg. combineLatest or zip).

A function that:

  • takes one value
  • and returns one new value of any type

may be used as a project function with map.

A function that:

  • takes one value
  • and returns one new value of type boolean (✔ true or ✘ false)

may be used as a predicate function on filtering operators (eg. filter, takeWhile or skipWhile)

As you may have noticed, a function used as a predicate can also be used as project function on a map operator:

In this example, the function nn == 1, that returns ✔ true or ✘ false, is used both as an argument for map as a project and for filter as a predicate. As a result:

  • map returns a new stream of boolean values, according to the result of this project function
  • filter returns a new stream of filtered values, where the values can pass only if this predicate returns ✔ true

Summary

  • Some operators may accept functions as arguments. These functions don’t have to work with the input or the output stream. The operators do.
  • How you label a function (eg. “project” or “predicate” function) depends on how you use it. A function is not a project or predicate function in itself.

See also


map vs filter


accumulator

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 →