Code example for collect and collectType (#27744)

Co-Authored-By: Helena Edelson <helena@users.noreply.github.com>
This commit is contained in:
Patrik Nordwall 2019-09-19 11:22:37 +02:00 committed by GitHub
parent edad69b38c
commit d8c8121c4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 143 additions and 5 deletions

View file

@ -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
}
}