doc: filter and filterNot streams operator, #25468
This commit is contained in:
parent
4b632c4537
commit
5f21c2264b
4 changed files with 114 additions and 0 deletions
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue