Skip to content

Latest commit

 

History

History
65 lines (39 loc) · 2.65 KB

predicate.md

File metadata and controls

65 lines (39 loc) · 2.65 KB
id lesson title layout class preview_image preview_image_alt
predicate
24
Reactive Programming - Operators and functions (Part 1)
default
post
predicate/content_preview.jpg
project function vs predicate function

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:

{:.w450}

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

{:.w300}
map vs filter

{:.w300}
accumulator