A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://reactivex.io/documentation/operators/takewhile.html below:

ReactiveX - TakeWhile operator

  1. Operators
  2. Conditional and Boolean
  3. TakeWhile
TakeWhile mirror items emitted by an Observable until a specified condition becomes false

The TakeWhile mirrors the source Observable until such time as some condition you specify becomes false, at which point TakeWhile stops mirroring the source Observable and terminates its own Observable.

See Also Language-Specific Information:

The takeWhile operator returns an Observable that mirrors the behavior of the source Observable until such time as a function, applied to an item emitted by that Observable, returns false, whereupon the new Observable terminates with an onCompleted notification.

numbers = Observable.from( [1, 2, 3, 4, 5, 6, 7, 8, 9] );

numbers.takeWhile({ ((it < 6) || (0 == (it % 2))) }).subscribe(
  { println(it); },                          // onNext
  { println("Error: " + it.getMessage()); }, // onError
  { println("Sequence complete"); }          // onCompleted
);
1
2
3
4
5
6
Sequence complete

takeWhile does not by default operate on any particular Scheduler.

The takeWhile operator returns an Observable that mirrors the behavior of the source Observable until such time as a function, applied to an item emitted by that Observable, returns false, whereupon the new Observable terminates with an onCompleted notification.

takeWhile does not by default operate on any particular Scheduler.

RxJS implements the takeWhile operator. You pass it a function that governs the takeping process. takeWhile calls that function for each item emitted by the source Observable until such time as the function returns false, whereupon takeWhile stops mirroring the source Observable (starting with that item) and issues an onCompleted notification. The function takes three parameters:

  1. the emitted item
  2. the zero-based index of that item in the sequence of emissions
  3. the source Observable

You may optionally pass a second parameter to takeWhile. If so, that item will also be available to your predicate function as “this”.

Sample Code
var source = Rx.Observable.range(1, 5)
    .takeWhile(function (x) { return x < 3; });

var subscription = source.subscribe(
    function (x) { console.log('Next: ' + x); },
    function (err) { console.log('Error: ' + err); },
    function () { console.log('Completed'); });
Next: 0
Next: 1
Next: 2
Completed

takeWhile is found in each of the following distributions:

RxPHP implements this operator as takeWhile.

Returns elements from an observable sequence as long as a specified condition is true. It takes as a parameter a a callback to test each source element for a condition. The callback predicate is called with the value of the element.

Sample Code
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/take/takeWhile.php


$source = \Rx\Observable::range(1, 5)
    ->takeWhile(function ($x) {
        return $x < 3;
    });

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


   
Next value: 1
Next value: 2
Complete!
    

RxPHP also has an operator takeWhileWithIndex.

Returns elements from an observable sequence as long as a specified condition is true. It takes as a parameter a a callback to test each source element for a condition. The callback predicate is called with the index and the value of the element.

Sample Code
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/take/takeWhileWithIndex.php


$source = \Rx\Observable::range(1, 5)
    ->takeWhileWithIndex(function ($i) {
        return $i < 3;
    });

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


   
Next value: 1
Next value: 2
Next value: 3
Complete!
    

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4