The Filter operator filters an Observable by only allowing items through that pass a test that you specify in the form of a predicate function.
filter
TBD
filter ofType
RxGroovy implements this operator as filter. You can filter an Observable, discarding any items that do not meet some test, by passing a filtering function into the filter operator. For example, the following code filters a list of integers, emitting only those that are even (that is, where the remainder from dividing the number by two is zero):
numbers = Observable.from([1, 2, 3, 4, 5, 6, 7, 8, 9]); numbers.filter({ 0 == (it % 2) }).subscribe( { println(it); }, // onNext { println("Error: " + it.getMessage()); }, // onError { println("Sequence complete"); } // onCompleted );
2 4 6 8 Sequence complete
filter does not by default operate on any particular Scheduler.
filter(Func1)
There is also a specialized form of the Filter operator in RxGroovy that filters an Observable so that it only emits items of a particular class.
ofType does not by default operate on any particular Scheduler.
ofType
ofType(Class)
RxJava implements this operator as filter.
Observable.just(1, 2, 3, 4, 5) .filter(new Func1<Integer, Boolean>() { @Override public Boolean call(Integer item) { return( item < 4 ); } }).subscribe(new Subscriber<Integer>() { @Override public void onNext(Integer item) { System.out.println("Next: " + item); } @Override public void onError(Throwable error) { System.err.println("Error: " + error.getMessage()); } @Override public void onCompleted() { System.out.println("Sequence complete."); } });
Next: 1 Next: 2 Next: 3 Sequence complete.
filter where
RxJS implements this operator under two names, but with identical behavior: filter and where. This operator takes two parameters: the predicate function, and an optional object that will represent that function’s “this” context when it executes.
where
this
The predicate function itself takes three arguments:
Write the predicate function so that it returns true for those items you want to pass through the filter to the next observer, and false for those items you want the filter to block and suppress.
true
false
var source = Rx.Observable.range(0, 5) .filter(function (x, idx, obs) { return x % 2 === 0; }); var subscription = source.subscribe( function (x) { console.log('Next: %s', x); }, function (err) { console.log('Error: %s', err); }, function () { console.log('Completed'); });
Next: 0 Next: 2 Next: 4 Completed
filter and where are found in each of the following distributions:
rx.js
rx.all.js
rx.all.compat.js
rx.compat.js
rx.lite.js
rx.lite.compat.js
OfType Where
RxPHP implements this operator as filter.
Filters the elements of an observable sequence based on a predicate by incorporating the element's index.
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/filter/filter.php $observable = Rx\Observable::fromArray([21, 42, 84]); $observable ->filter(function ($elem) { return $elem >= 42; }) ->subscribe($stdoutObserver);
Next value: 42 Next value: 84 Complete!
RxPHP also has an operator where.
Alias for filter
filter slice where
select select_with_index
collect filter filterNot BlockingObservable.withFilter withFilter