zipWith

Combines elements from multiple sources through a combine function and passes the returned value downstream.

Fan-in operators

Signature

def zipWith[Out2, Out3](that: Graph[SourceShape[Out2], _])(combine: (Out, Out2) => Out3): Repr[Out3]
def zipWithMat[Out2, Out3, Mat2, Mat3](that: Graph[SourceShape[Out2], Mat2])(combine: (Out, Out2) => Out3)(matF: (Mat, Mat2) => Mat3): ReprMat[Out3, Mat3]

Description

Combines elements from multiple sources through a combine function and passes the returned value downstream.

emits when all of the inputs have an element available

backpressures when downstream backpressures

completes when any upstream completes

Example

Scala
sourceimport akka.stream.scaladsl.Source
import akka.stream.scaladsl.Sink

    val sourceCount = Source(List("one", "two", "three"))
    val sourceFruits = Source(List("apple", "orange", "banana"))

    sourceCount
      .zipWith(sourceFruits) { (countStr, fruitName) =>
        s"$countStr $fruitName"
      }
      .runWith(Sink.foreach(println))
    // this will print 'one apple', 'two orange', 'three banana'
Java
sourceimport akka.stream.javadsl.Source;
import akka.stream.javadsl.Sink;
import java.util.Arrays;

Source<String, NotUsed> sourceCount = Source.from(Arrays.asList("one", "two", "three"));
Source<String, NotUsed> sourceFruits = Source.from(Arrays.asList("apple", "orange", "banana"));
sourceCount
    .zipWith(
        sourceFruits,
        (Function2<String, String, String>) (countStr, fruitName) -> countStr + " " + fruitName)
    .runWith(Sink.foreach(System.out::print), materializer);
// this will print 'one apple', 'two orange', 'three banana'
Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.