intersperse
Intersperse stream with provided element similar to List.mkString
.
Signature
Source.intersperse
Source.intersperse
Flow.intersperse
Flow.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
-
source
Source(1 to 4).map(_.toString).intersperse("[", ", ", "]").runWith(Sink.foreach(print)) // prints // [1, 2, 3, 4]
- Java
-
source
Source.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