interleave

Emits a specifiable number of elements from the original source, then from the provided source and repeats.

Fan-in operators

Signature

def interleave[U >: Out](that: Graph[SourceShape[U], _], segmentSize: Int): Repr[U]
def interleave[U >: Out](that: Graph[SourceShape[U], _], segmentSize: Int, eagerClose: Boolean): Repr[U]
def interleaveMat[U >: Out, Mat2, Mat3](that: Graph[SourceShape[U], Mat2], request: Int)(matF: (Mat, Mat2) => Mat3): ReprMat[U, Mat3]
def interleaveMat[U >: Out, Mat2, Mat3](that: Graph[SourceShape[U], Mat2], request: Int, eagerClose: Boolean)(matF: (Mat, Mat2) => Mat3): ReprMat[U, Mat3]

Description

Emits a specifiable number of elements from the original source, then from the provided source and repeats. If one source completes the rest of the other stream will be emitted.

emits when element is available from the currently consumed upstream

backpressures when upstream backpressures

completes when both upstreams have completed

Example

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

val sourceA = Source(List(1, 2, 3, 4))
val sourceB = Source(List(10, 20, 30, 40))

sourceA.interleave(sourceB, segmentSize = 2).runWith(Sink.foreach(println))
//prints 1, 2, 10, 20, 3, 4, 30, 40
Java
sourceimport akka.stream.javadsl.Source;
import akka.stream.javadsl.Sink;
import java.util.Arrays;

Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 2, 3, 4));
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(10, 20, 30, 40));
sourceA.interleave(sourceB, 2).runWith(Sink.foreach(System.out::print), materializer);
// prints 1, 2, 10, 20, 3, 4, 30, 40
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.