takeWhile

Pass elements downstream as long as a predicate function returns true and then complete.

Simple operators

Signature

def takeWhile(p: Out => Boolean): Repr[Out]
def takeWhile(p: Out => Boolean, inclusive: Boolean): Repr[Out]

Description

Pass elements downstream as long as a predicate function returns true and then complete. The element for which the predicate returns false is not emitted.

Example

Scala
Source(1 to 10).takeWhile(_ < 3).runForeach(println)
// prints
// 1
// 2
Java
Source.from(Arrays.asList(1, 2, 3, 4, 5))
    .takeWhile(i -> i < 3)
    .runForeach(System.out::println, system);
// this will print:
// 1
// 2

Reactive Streams semantics

emits while the predicate is true and until the first false result

backpressures when downstream backpressures

completes when predicate returned false or upstream completes

Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.