Skip to content

Instantly share code, notes, and snippets.

@raloliver
Last active September 12, 2019 15:34
Show Gist options
  • Save raloliver/b36402e4cc0bb0a095f327d9f09f726f to your computer and use it in GitHub Desktop.
Save raloliver/b36402e4cc0bb0a095f327d9f09f726f to your computer and use it in GitHub Desktop.
RxJS: count, max, min, reduce (math operators)
var sourceCountEach = Rx.Observable.range(0, 10).count()
var subscriptionCountEach = source.subscribe(
value => console.log(value)
)
var sourceCountOdd = Rx.Observable.range(0, 10).count(x => x % 2 === 0)
var subscriptionCountOdd = source.subscribe(
value => console.log(value)
)
var sourceMaxVal = Rx.Observable.from([1,3,5,7,9,2,4,6,8]).max();
var subscriptionMaxVal = source.subscribe(
value => console.log(value)
)
var sourceMinVal = Rx.Observable.from([1,3,5,7,9,2,4,6,8]).min();
var subscriptionMinVal = source.subscribe(
value => console.log(value)
)
//Como podemos obter a soma de todos os elementos com “reduce()”, a função “sum()” foi removida na versão 5.0 da biblioteca.
var sourceReduce = Rx.Observable.from([1,2,3,4,5]).reduce((total, current) => total + current );
var subscriptionReduce = source.subscribe(
value => console.log(value)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment