dimap

Transform this Flow by applying a function f to each incoming upstream element before it is passed to the Flow, and a function g to each outgoing downstream element.

Simple operators

Signature

Flow.dimapFlow.dimap

Description

Transform this Flow by applying a function f to each incoming upstream element before it is passed to the Flow, and a function g to each outgoing downstream element.

Examples

Scala
sourceimport org.apache.pekko
import pekko.NotUsed
import pekko.actor.ActorSystem
import pekko.stream.scaladsl._
val source = Source(List("1", "2", "3"))
val flow: Flow[Int, Int, NotUsed] = Flow[Int].map(_ * 2)
val newFlow: Flow[String, String, NotUsed] = flow.dimap(Integer.parseInt)(_.toString)
source.via(newFlow).runForeach(println)
// expected prints:
// 2
// 4
// 6
Java
sourceimport org.apache.pekko.NotUsed;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.stream.javadsl.Flow;
import org.apache.pekko.stream.javadsl.Source;

import java.util.Arrays;
final Source<String, NotUsed> source = Source.from(Arrays.asList("1", "2", "3"));
final Flow<Integer, Integer, NotUsed> flow = Flow.<Integer>create().map(elem -> elem * 2);
source
    .via(flow.dimap(Integer::parseInt, String::valueOf))
    .runForeach(System.out::println, system);
// expected prints:
// 2
// 4
// 6

Reactive Streams semantics

emits when the mapping function g returns an element

backpressures ’‘‘Backpressures when’’’ original flow backpressures

completes when original flow completes

cancels when original flow cancels