diff --git a/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/filter.md b/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/filter.md index 92357dcdc6..d335ce82f2 100644 --- a/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/filter.md +++ b/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/filter.md @@ -17,6 +17,18 @@ Filter the incoming elements using a predicate. Filter the incoming elements using a predicate. If the predicate returns true the element is passed downstream, if it returns false the element is discarded. +See also @ref:[`filterNot`](filterNot.md). + +## Example + +For example, given a `Source` of words we can select the longer words with the `filter` operator: + +Scala +: @@snip [Filter.scala](/akka-docs/src/test/scala/docs/stream/operators/sourceorflow/Filter.scala) { #filter } + +Java +: @@snip [SourceOrFlow.java](/akka-docs/src/test/java/jdocs/stream/operators/SourceOrFlow.java) { #filter } + ## Reactive Streams semantics @@@div { .callout } diff --git a/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/filterNot.md b/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/filterNot.md index c994accfde..3b827de0a5 100644 --- a/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/filterNot.md +++ b/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/filterNot.md @@ -17,6 +17,18 @@ Filter the incoming elements using a predicate. Filter the incoming elements using a predicate. If the predicate returns false the element is passed downstream, if it returns true the element is discarded. +See also @ref:[`filter`](filter.md). + +## Example + +For example, given a `Source` of words we can omit the shorter words with the `filterNot` operator: + +Scala +: @@snip [Filter.scala](/akka-docs/src/test/scala/docs/stream/operators/sourceorflow/Filter.scala) { #filterNot } + +Java +: @@snip [SourceOrFlow.java](/akka-docs/src/test/java/jdocs/stream/operators/SourceOrFlow.java) { #filterNot } + ## Reactive Streams semantics @@@div { .callout } diff --git a/akka-docs/src/test/java/jdocs/stream/operators/SourceOrFlow.java b/akka-docs/src/test/java/jdocs/stream/operators/SourceOrFlow.java index 67a45ad040..b5a515908f 100644 --- a/akka-docs/src/test/java/jdocs/stream/operators/SourceOrFlow.java +++ b/akka-docs/src/test/java/jdocs/stream/operators/SourceOrFlow.java @@ -348,4 +348,44 @@ class SourceOrFlow { // 2 // #take-while } + + void filterExample() { + // #filter + Source words = + Source.from( + Arrays.asList( + ("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt " + + "ut labore et dolore magna aliqua.") + .split(" "))); + + Source longWords = words.filter(w -> w.length() > 6); + + longWords.runWith(Sink.foreach(System.out::print), system); + // consectetur + // adipiscing + // eiusmod + // tempor + // incididunt + // #filter + } + + void filterNotExample() { + // #filterNot + Source words = + Source.from( + Arrays.asList( + ("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt " + + "ut labore et dolore magna aliqua.") + .split(" "))); + + Source longWords = words.filterNot(w -> w.length() <= 5); + + longWords.runWith(Sink.foreach(System.out::print), system); + // consectetur + // adipiscing + // eiusmod + // tempor + // incididunt + // #filterNot + } } diff --git a/akka-docs/src/test/scala/docs/stream/operators/sourceorflow/Filter.scala b/akka-docs/src/test/scala/docs/stream/operators/sourceorflow/Filter.scala new file mode 100644 index 0000000000..fbb26a9d71 --- /dev/null +++ b/akka-docs/src/test/scala/docs/stream/operators/sourceorflow/Filter.scala @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2019 Lightbend Inc. + */ + +package docs.stream.operators.sourceorflow + +import akka.NotUsed +import akka.actor.ActorSystem +import akka.stream.scaladsl.Source + +object Filter { + + implicit val system: ActorSystem = ActorSystem() + + def filterExample(): Unit = { + // #filter + val words: Source[String, NotUsed] = + Source( + ("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt " + + "ut labore et dolore magna aliqua.").split(" ").toList) + + val longWords: Source[String, NotUsed] = words.filter(_.length > 6) + + longWords.runForeach(println) + // consectetur + // adipiscing + // eiusmod + // tempor + // incididunt + // #filter + } + + def filterNotExample(): Unit = { + // #filterNot + val words: Source[String, NotUsed] = + Source( + ("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt " + + "ut labore et dolore magna aliqua.").split(" ").toList) + + val longWords: Source[String, NotUsed] = words.filterNot(_.length <= 5) + + longWords.runForeach(println) + // consectetur + // adipiscing + // eiusmod + // tempor + // incididunt + // #filterNot + } +}