Member-only story

Kotlin Flow — Combine, Merge and Zip

Exploring the Power of Kotlin Flow: Combining, Merging, and Zipping Streams

Vincent Tsen
3 min readMay 20, 2023

This is part of the asynchronous flow series:

I have 2 flows here.

Flow1 emits int starting from 1 -> 1000 every 1 second.

private val flow1: Flow<Int> = flow {
repeat(10000) { value ->
delay(1000)
Log.d(tag, "[Flow1]: emitting $value")
emit(value)
}
}

Flow 2 emits char from A to Z every 2 seconds.

private val flow2: Flow<Char> = flow {
var value = 'A'
while(true) {
delay(2000)
Log.d(tag, "[Flow2]: emitting $value")
emit(value)
if (value == 'Z') {
value = 'A'
} else {
value += 1
}
}
}

Combine Flow

--

--

No responses yet