When we work with streams, we often want to refine the values in a stream with some logic. In Bacon.js, combinators enable refinement.
Combinators let us build complex structures with simple functions. Combinators solve the same problem as the composition pattern in object-oriented programming.
For example, this is how we can use the map
combinator:
doubledValue = Bacon.once(1).map(number => number * 2)
map
– transforms values in the streamfilter
– lets through values in the streamflatMap
– spawns a stream with a new sourcecombine
– combines the latest values of two streams using a two-arg functionmerge
– merges two streams into one stream that delivers events from bothThere are many other combinators in Bacon.js, but the above ones are the fundamental combinators. Once you understand them, you can rather easily understand all the other combinators that you encounter.
map
combinatorCreate a stream that sequentially emits the numbers 1, 2 and 3. Then transform
that stream with the map
combinator such that the resulting stream will emit
the values 1, 8 and 27.
filter
combinatorAgain, create a stream that sequentially emits the numbers 1, 2 and 3. With the
help of the filter
combinator, let through only values that are even.
flatMap
combinatorThe flatMap
combinator captures a fundamental programming concept. Let’s take
a closer look at it.