intersperse

fun <T> Flow<T>.intersperse(between: T, start: T? = null, end: T? = null): Flow<T>

Intersperses the elements of the Flow with the specified start, between, and end elements.

Return

A new Flow with the start, between, and end elements inserted between and around the original flow elements.

Example usage:

flowOf(1, 2, 3)
.intersperse(start = 0, between = -1, end = 4)
.collect(::println) //0, 1, -1, 2, -1, 3, 4

Parameters

between

An element of type T to insert between the flow elements.

start

An optional element of type T to insert at the beginning of the flow. Defaults to null.

end

An optional element of type T to insert at the end of the flow. Defaults to null.