Code example for collect and collectType (#27744)
Co-Authored-By: Helena Edelson <helena@users.noreply.github.com>
This commit is contained in:
parent
edad69b38c
commit
d8c8121c4d
4 changed files with 143 additions and 5 deletions
|
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package docs.stream.operators.sourceorflow
|
||||
|
||||
import akka.NotUsed
|
||||
import akka.stream.scaladsl.Flow
|
||||
|
||||
object Collect {
|
||||
//#collect-elements
|
||||
trait Message
|
||||
final case class Ping(id: Int) extends Message
|
||||
final case class Pong(id: Int)
|
||||
//#collect-elements
|
||||
|
||||
def collectExample(): Unit = {
|
||||
//#collect
|
||||
val flow: Flow[Message, Pong, NotUsed] =
|
||||
Flow[Message].collect {
|
||||
case Ping(id) if id != 0 => Pong(id)
|
||||
}
|
||||
//#collect
|
||||
}
|
||||
|
||||
def collectType(): Unit = {
|
||||
//#collectType
|
||||
val flow: Flow[Message, Pong, NotUsed] =
|
||||
Flow[Message].collectType[Ping].filter(_.id != 0).map(p => Pong(p.id))
|
||||
//#collectType
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue