Last active
April 4, 2021 10:45
-
-
Save NyaGarcia/f03d3e6217a974c41d9af5fbc68809da to your computer and use it in GitHub Desktop.
Duplicated logic in Observables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { from } from "rxjs"; | |
import { filter, reduce } from "rxjs/operators"; | |
const number$ = from([null, 2, 1, undefined, 5, false, 6, 7]); | |
// Adding numbers | |
number$ | |
.pipe( | |
filter<number>(Boolean), | |
reduce((acc, curr) => acc + curr) | |
) | |
.subscribe(n => console.log(`Total: ${n}`)); | |
// Emit even numbers | |
number$ | |
.pipe( | |
filter<number>(Boolean), | |
filter(n => n % 2 === 0) | |
) | |
.subscribe(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment