pipe()
is a utility for chaining transformations together. It actually has nothing to do
with callbags specifically, its just particularly useful with callbag utilities.
1linkfunction pipe<A, B, C>(
2link a: A,
3link b: (a: A) => B,
4link c: (b: B) => C
5link): C
This code:
1linkimport { pipe, interval, map, filter, subscribe } from 'callbag-common';
2link
3linkpipe(
4link interval(1000),
5link map(x => x * 3),
6link filter(x => x % 2 === 0),
7link subscribe(console.log)
8link)
Is equivalent to this code:
1linkimport { interval, map, filter, subscribe } from 'callbag-common';
2link
3linklet source = interval(1000);
4linksource = map(x => x * 3)(source);
5linksource = filter(x => x % 2 === 0)(source);
6linksubscribe(console.log)(source);
In other words:
1linkpipe(a, b, c, d) === d(c(b(a)))
👉 You can also use pipe()
to easily build new utilities and operators:
1link​const multiply = k => src => pipe(src, map(n => n * k));
2link​
3link​pipe(
4link​ interval(1000),
5link​ multiply(10),
6link​ subscribe(console.log)
7link​)
8link​
0
10
20
30
...