intersperse

Intersperse stream with provided element similar to List.mkString.

Simple operators

Signature

Source.intersperseSource.intersperse Flow.intersperseFlow.intersperse

Description

Intersperse stream with provided element similar to List.mkString. It can inject start and end marker elements to stream.

Example

The following takes a stream of integers, converts them to strings and then adds a [ at the start, , between each element and a ] at the end.

Scala
sourceSource(1 to 4).map(_.toString).intersperse("[", ", ", "]").runWith(Sink.foreach(print))
// prints
// [1, 2, 3, 4]
Java
sourceSource.from(Arrays.asList(1, 2, 3))
    .map(String::valueOf)
    .intersperse("[", ", ", "]")
    .runForeach(System.out::print, system);
// prints
// [1, 2, 3]

Reactive Streams semantics

emits when upstream emits an element or before with the start element if provided

backpressures when downstream backpressures

completes when upstream completes

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.