Star

Created With

linkNumeric Operations

Numeric operations allow you to do numeric arithmetic on observables.


linkmod()

Calculates the modulo of two (or more) observables or numbers:

1import { mod } from 'rxmetics';

2import { interval } from 'rxjs';

3

4mod(interval(1000), 4).subscribe(console.log);

5

6// Result:

7// > 0, 1, 2, 3, 0, 1, 2, 3, ...

play_arrow

You can also use the mod() pipe instead:

1import { mod } from 'rxmetics/pipes';

2import { interval } from 'rxjs';

3

4interval(1000).pipe(mod(4)).subscribe(console.log);

5

6// Result:

7// > 0, 1, 2, 3, 0, 1, 2, 3, ...


linkadd()

Adds two (or more) observables or numbers:

1import { add } from 'rxmetics';

2import { mod } from 'rxmetics/pipes';

3import { interval } from 'rxjs';

4

5add(interval(1000), interval(300).pipe(mod(3)), 10).subscribe(console.log);

6

7// Result:

8// > 12, 10, 11, 12, 13, 11, 12, 13, 14, ...

play_arrow

You can also use the add() pipe instead:

1import { add, mod } from 'rxmetics/pipes';

2import { interval } from 'rxjs';

3

4interval(1000).pipe(

5 add(interval(300).pipe(mod(3)), 10),

6 add(1000),

7).subscribe(console.log);

8

9// Result:

10// > 1012, 1010, 1011, 1012, 1013, 1011, 1012, 1013, 1014, ...


linksub()

Subtracts two (or more) observables or numbers:

1import { sub } from 'rxmetics';

2import { fromEvent } from 'rxjs';

3import { pluck } from 'rxjs/operators';

4

5const mx = fromEvent(document, 'mousemove').pipe(pluck('clientX'));

6sub(mx, window.innerWidth / 2).subscribe(v => document.body.textContent = v);

play_arrow

You can also use sub() pipe instead:

1import { sub } from 'rxmetics/pipes';

2import { fromEvent } from 'rxjs';

3import { pluck } from 'rxjs/operators';

4

5const mx = fromEvent(document, 'mousemove').pipe(

6 pluck('clientX'),

7 sub(window.innerWidth / 2)

8).subscribe(v => document.textContent = v);


linkmul()

Multiplies two (or more) observables or numbers:

1import { mul } from 'rxmetics';

2import { sub } from 'rxmetics/pipes';

3import { fromEvent } from 'rxjs';

4import { pluck } from 'rxjs/operators';

5

6const dx = fromEvent(document, 'mousemove').pipe(

7 pluck('clientX'),

8 sub(window.innerWidth / 2)

9);

10

11mul(dx, dx).subscribe(v => document.body.textContent = v);

play_arrow

You can also use the mul() pipe instead:

1import { mul, sub } from 'rxmetics';

2import { fromEvent } from 'rxjs';

3import { pluck } from 'rxjs/operators';

4

5const dx = fromEvent(document, 'mousemove').pipe(

6 pluck('clientX'),

7 sub(window.innerWidth / 2)

8);

9

10dx.pipe(mul(dx)).subscribe(v => document.body.textContent = v);


linkdiv()

Divides two (or more) observables or numbers:

1import { div } from 'rxmetics';

2import { sub, mul } from 'rxmetics/pipes';

3import { fromEvent } from 'rxjs';

4import { pluck } from 'rxjs/operators';

5

6const dx = fromEvent(document, 'mousemove').pipe(

7 pluck('clientX'),

8 sub(window.innerWidth / 2)

9);

10

11const dx2 = dx.pipe(mul(dx));

12div(dx2, window.innerWidth * window.innerWidth / 4)

13 .subscribe(v => document.body.textContent = v);

play_arrow

You can also use the div() pipe instead:

1import { sub, mul, div } from 'rxmetics/pipes';

2import { fromEvent } from 'rxjs';

3import { pluck } from 'rxjs/operators';

4

5const dx = fromEvent(document, 'mousemove').pipe(

6 pluck('clientX'),

7 sub(window.innerWidth / 2)

8);

9

10dx.pipe(

11 mul(dx),

12 div(window.innerWidth * window.innerWidth / 4),

13).subscribe(v => document.body.textContent = v);


linkneg()

Returns negative of incoming numbers:

1 import { neg } from 'rxmetics';

2import { interval } from 'rxjs';

3

4neg(interval(1000)).subscribe(console.log);

5

6// Result:

7// > -1, -2, -3, ...

Can also be used as a pipe:

1 import { neg } from 'rxmetics'; // --> or import it from 'rxmetics/pipes', doesn't matter.

2import { interval } from 'rxjs';

3

4interval(1000).pipe(neg).subscribe(console.log);

5

6// Result:

7// > -1, -2, -3, ...


linkinv()

Inverts incoming numbers:

1 import { inv } from 'rxmetics';

2import { add } from 'rxmetics/pipes';

3import { interval } from 'rxjs';

4

5inv(interval(1000).pipe(add(1))).subscribe(console.log);

6

7// Result:

8// > 1, 0.5, 0.333333333, 0.25, 0.2, ...

Can also be used as a pipe:

1 import { inv } from 'rxmetics'; // --> or import it from 'rxmetics/pipes', doesn't matter.

2import { add } from 'rxmetics/pipes';

3import { interval } from 'rxjs';

4

5interval(1000).pipe(add(1), inv).subscribe(console.log);

6

7// Result:

8// > 1, 0.5, 0.333333333, 0.25, 0.2, ...


Numeric Operationsmod()add()sub()mul()div()neg()inv()

Home Numeric Operations String Operations Boolean Operations Generic Functions