Count

count the number of items emitted by the source Observable and emit only this value

The Count operator transforms an Observable that emits items into an Observable that emits a single value that represents the number of items emitted by the source Observable.

If the source Observable terminates with an error, Count will pass this error notification along without emitting an item first. If the source Observable does not terminate at all, Count will neither emit an item nor terminate.

See Also

Language-Specific Information:

RxClojure has both count and longCount variants of this operator, but both of these in fact return long values.

RxCpp implements this operator as count.

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.

Sample Code

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

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.

Sample Code

String[] items = new String[] { "one", "two", "three" };
assertEquals( new Integer(3), Observable.from(items).count().toBlocking().single() );

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:

  1. the emitted item
  2. the index of that item in the sequence of emitted items
  3. the source Observable

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:

Sample Code

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

RxKotlin implements this operator as count.

Sample Code

val list = listOf(1, 2, 3, 4, 5)
assertEquals( 5, Observable.from(list)!!.count()!!.toBlocking()!!.single() )

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).

RxPHP implements this operator as count.

Returns an observable sequence containing a value that represents how many elements in the specified observable sequence satisfy a condition if provided, else the count of items.

Sample Code

//from https://github.com/ReactiveX/RxPHP/blob/master/demo/count/count.php

$source = \Rx\Observable::fromArray(range(1, 10));

$subscription = $source->count()->subscribe($stdoutObserver);

   
Next value: 10
Complete!
    

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.

Sample Code

numberOfItems = someObservable.count()
numberOfNegativeItems = someObservable.count(lambda x: x < 0)

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.

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.