Star

Created With


expr



Calculate expressions with values from multiple callbag sources:

1linkfunction expr<R>(fn: ($: Track) => R): Source<R>

1linkimport { fromEvent, pipe, subscribe, expr, map } from 'callbag-common';

2link

3linkconst span = document.querySelector('span');

4linkconst a = document.querySelector('#a') as HTMLInputElement;

5linkconst b = document.querySelector('#b') as HTMLInputElement;

6link

7linkconst aVal = pipe(fromEvent(a, 'input'), map(() => parseInt(a.value)));

8linkconst bVal = pipe(fromEvent(b, 'input'), map(() => parseInt(b.value)));

9link

10linkpipe(

11link expr($ => $(aVal) + $(bVal)),

12link subscribe(v => span.textContent = v)

13link)

► Try It!
1linkimport { interval, pipe, subscribe, expr } from 'callbag-common';

2link

3linkconst span = document.querySelector('span');

4link

5linkconst a = interval(50);

6linkconst b = interval(200);

7linkconst c = interval(3000);

8link

9linkpipe(

10link expr($ => {

11link if ($(c) % 2 === 0) return 'A = ' + $(a);

12link else return 'B = ' + $(b);

13link }),

14link subscribe(v => span.textContent = v)

15link)

► Try It!


👉 A key point in using expr() is that you SHOULD NOT create new callbag sources in the function you give it. So this is wrong:

1linkexpr~~~~(~$ ~~=>~~ ~$~(~interval~~~~~~~~(~1000~~~~)~)~ ~*~ ~2~)~;~

And this is the correct version:

1linkconst i = interval(1000);

2linkexpr($ => $(i) * 2);




Home What Are Callbags?

Source Factorieschevron_right
Operatorschevron_right
Combinationchevron_right
Utilitieschevron_right