-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
152 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,47 @@ | ||
# exists | ||
# Sink.exists | ||
|
||
A `Sink` that will test the given predicate `p` for every received element and completes with the result. | ||
|
||
Completes the stream with true as soon as there's an element satisfy the *predicate*, or emits false when the stream | ||
completes and no element satisfied the *predicate*. | ||
@ref[Sink operators](../index.md#sink-operators) | ||
|
||
## Signature | ||
|
||
@apidoc[Flow.exists](Flow$) { scala="#exists[I](p:Out%20=%3E%20Boolean)" java="#exists[I](p:org.apache.pekko.japi.function.Predicate[I])" } | ||
|
||
@apidoc[Sink.exists](Sink$) { scala="#exists[I](p:Out%20=%3E%20Boolean)" java="#exists[I](p:org.apache.pekko.japi.function.Predicate[I])" } | ||
@apidoc[Sink.exists](Sink$) { scala="#exists[T](p:T=%3EBoolean):org.apache.pekko.stream.scaladsl.Sink[T,scala.concurrent.Future[Boolean]]" java="#exists(org.apache.pekko.japi.function.Predicate)" } | ||
|
||
## Description | ||
`exists` applies a predicate function to assert each element received, it returns true if any elements satisfy the assertion, otherwise it returns false. | ||
|
||
It materializes into a `Future` (in Scala) or a `CompletionStage` (in Java) that completes with the last state when the stream has finished. | ||
|
||
Notes that if source is empty, it will return false | ||
|
||
A `Sink` that will test the given predicate `p` for every received element and | ||
|
||
- completes and returns @scala[`Future`] @java[`CompletionStage`] of `true` if the predicate is true for any element; | ||
- completes and returns @scala[`Future`] @java[`CompletionStage`] of `false` if the stream is empty (i.e. completes before signalling any elements); | ||
- completes and returns @scala[`Future`] @java[`CompletionStage`] of `false` if the predicate is false for all elements. | ||
|
||
The materialized value @scala[`Future`] @java[`CompletionStage`] will be completed with the value `true` or `false` | ||
when the input stream ends, or completed with `Failure` if there is a failure signaled in the stream. | ||
|
||
Completes the stream with true as soon as there's an element satisfy the *predicate*, or emits false when the stream | ||
completes and no element satisfied the *predicate*. | ||
## Example | ||
|
||
## Examples | ||
This example tests any element in the stream is `>` 3. | ||
|
||
Scala | ||
: @@snip [Exists.scala](/docs/src/test/scala/docs/stream/operators/sink/Exists.scala) { #imports #exists } | ||
: @@snip [exists.scala](/docs/src/test/scala/docs/stream/operators/sink/Exists.scala) { #exists } | ||
|
||
Java | ||
: @@snip [Exists.java](/docs/src/test/java/jdocs/stream/operators/sink/Exists.java) { #imports #exists } | ||
: @@snip [exists.java](/docs/src/test/java/jdocs/stream/operators/sink/Exists.java) { #exists } | ||
|
||
## Reactive Streams Semantics | ||
|
||
@@@div { .callout } | ||
|
||
***Completes*** when upstream completes or the predicate `p` returns `true` | ||
|
||
**cancels** when predicate `p` returns `true` | ||
|
||
**backpressures** when the invocation of predicate `p` has not yet completed | ||
|
||
@@@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,4 +44,4 @@ Java | |
|
||
**backpressures** when the invocation of predicate `p` has not yet completed | ||
|
||
@@@ | ||
@@@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters