count the number of items emitted by the source Observable and emit only this value
count longCount
RxClojure has both count
and longCount
variants of this operator, but both of these in fact return long values.
count
RxCpp implements this operator as count
.
count countLong
In RxGroovy the operator is called count
and the Observable it creates emits an Integer value. There is also a countLong
whose Observable emits a Long value.
def myObservable = Observable.create({ aSubscriber -> if(false == aSubscriber.isUnsubscribed()) aSubscriber.onNext('Three'); if(false == aSubscriber.isUnsubscribed()) aSubscriber.onNext('Two'); if(false == aSubscriber.isUnsubscribed()) aSubscriber.onNext('One'); if(false == aSubscriber.isUnsubscribed()) aSubscriber.onCompleted(); }); myObservable.count().subscribe( { println(it); }, // onNext { println("Error: " + it.getMessage()); }, // onError { println("Sequence complete"); } // onCompleted );
3 Sequence complete
count()
countLong()
count countLong
In RxJava the operator is called count
and the Observable it creates emits an Integer value. There is also a countLong
whose Observable emits a Long value.
String[] items = new String[] { "one", "two", "three" }; assertEquals( new Integer(3), Observable.from(items).count().toBlocking().single() );
count()
countLong()
count
In RxJS the operator count
counts the number of items in the source Observable that satisfy a specified predicate. That predicate takes the form of a function that takes three parameters:
If the predicate function returns true
, count
will increment the tally of items that it will report when the source Observable completes. If you want to count all of the items emitted by the source Observable, simply pass count
a predicate that always returns true
:
numberOfItems = someObservable.count(function() { return true; });
count
is part of the following packages:
rx.all.js
rx.all.compat.js
rx.aggregates.js
count
requires one of any of the following packages:
rx.js
rx.compat.js
rx.lite.js
rx.lite.compat.js
count
RxKotlin implements this operator as count
.
val list = listOf(1, 2, 3, 4, 5) assertEquals( 5, Observable.from(list)!!.count()!!.toBlocking()!!.single() )
Count LongCount
In Rx.NET the Observable this operator creates emits an Integer value, but there is also a LongCount
whose Observable emits a Long value. With both variants, you can either pass the source Observable in to the operator as a parameter or you can call the operator as an instance method of the source Observable (in which case you omit the parameter).
count
count
In RxPY you have the option to pass count
a predicate that takes an item emitted by the source Observable as a parameter. If you do so, count
will emit a count only of those items from the source Observable that the predicate evaluates as true
. Otherwise, it will emit a count of all items emitted by the source Observable.
numberOfItems = someObservable.count() numberOfNegativeItems = someObservable.count(lambda x: x < 0)
count
In Rx.rb you have the option to pass count
a predicate that takes an item emitted by the source Observable as a parameter. If you do so, count
will emit a count only of those items from the source Observable that the predicate evaluates as true
. Otherwise it will emit a count of all items emitted by the source Observable.
count length longCount size
In RxScala the operator count
counts the number of items in the source Observable that satisfy a specified predicate. That predicate accepts an emitted item as a parameter and returns a Boolean. count
will emit a count of all items for which this predicate returned true
.
Use length
or size
instead if you want to count all of the items emitted by the source Observable and emit this count as an Integer, or use longCount
to emit it as a Long.
© ReactiveX contributors
Licensed under the Apache License 2.0.
http://reactivex.io/documentation/operators/count.html