mapOption

Transform each element in the stream by calling a mapping function with it and emits the contained item if present.

Simple operators

Signature

Source.mapOptionSource.mapOption Flow.mapOptionFlow.mapOption

Description

Transform each element in the stream by calling a mapping function with it and emits the contained item if present.

Examples

Scala
sourceimport org.apache.pekko
import org.apache.pekko.NotUsed
import org.apache.pekko.stream.scaladsl._

val source: Source[Int, NotUsed] = Source(1 to 10)
val mapped: Source[String, NotUsed] = source.mapOption(elem => if (elem % 2 == 0) Some(elem.toString) else None)

Reactive Streams semantics

emits when the mapping function returns and element present

backpressures when downstream backpressures

completes when upstream completes