Combining Operators
Combining Operators in Kotlin Flow
In this lesson, you’ll explore combining operators in Kotlin Flow. These operators let you merge and join multiple data flows. You’ll cover the following operators:
-
zip: This operator takes two flows and combines them into a single flow by merging corresponding elements based on their index. It’s useful when you want to pair items from different sources. -
combine: This operator combines multiple flows into one, emitting values when any of the source flows change. This is useful when you need to respond to multiple data streams simultaneously.
The zip Operator
Suppose you have two flows: one for different carrot types and another for various
carrot processing techniques. You can use zip to combine these flows into a single flow that pairs each carrot type
with a processing method:
val carrotTypes = flowOf("Nantes", "Imperator", "Chantenay")
val processingMethods = flowOf("Wash", "Peel", "Chop")
fun processCarrots(): Flow<Pair<String, String>> =
carrotTypes.zip(processingMethods) { type, method -> type to method }
.collect { pair ->
println("Carrot type: ${pair.first}, Processing method: ${pair.second}")
}
The code above prints:
Carrot type: Nantes, Processing method: Wash
Carrot type: Imperator, Processing method: Peel
Carrot type: Chantenay, Processing method: Chop
The zip operator creates a single flow by merging two source flows, with each pair of corresponding elements forming
a single item in the resulting flow. This operator is helpful when you need to combine related items from different
data streams.
The combine Operator
Now, suppose you have two flows: one for the current temperature in the carrot factory and another for the humidity level. You want to create a flow that emits the latest temperature and humidity when either changes:
val temperatureFlow = flowOf(25, 27, 29) // Degrees Celsius
val humidityFlow = flowOf(40, 45, 50) // Percent humidity
fun monitorCarrotFactoryConditions(): Flow<Pair<Int, Int>> =
temperatureFlow.combine(humidityFlow) { temp, humidity -> temp to humidity }
.collect { pair ->
println("Temperature: ${pair.first}°C, Humidity: ${pair.second}%")
}
The code above prints:
Temperature: 25°C, Humidity: 40%
Temperature: 27°C, Humidity: 45%
Temperature: 29°C, Humidity: 50%
The combine operator takes multiple flows and merges them into a single flow, emitting values whenever any of the
source flows produce a new item. This operator is useful when you want to track changes in real time from multiple sources.
As you can see, zip and combine seem pretty similar. However, zip waits for both flows to emit before combining them, while combine combines the latest emissions of each flow as soon as they occur.
Adding a delay to the previous example:
val temperatureFlow = flowOf(25, 27, 29).onEach { delay(300) } // Degrees Celsius with delay
val humidityFlow = flowOf(40, 45, 50).onEach { delay(400) } // Percent humidity with delay
fun monitorCarrotFactoryConditions(): Flow<Pair<Int, Int>> =
temperatureFlow.combine(humidityFlow) { temp, humidity -> temp to humidity }
.collect { pair ->
println("Temperature: ${pair.first}°C, Humidity: ${pair.second}%")
}
The code above prints:
Temperature: 25°C, Humidity: 40%
Temperature: 27°C, Humidity: 40%
Temperature: 27°C, Humidity: 45%
Temperature: 29°C, Humidity: 45%
Temperature: 29°C, Humidity: 50%
Wrap-Up
In this lesson, you’ve explored key combining operators in Kotlin Flow, including zip and combine. You’ve seen
how these operators can be used to merge multiple flows, letting you create more complex data processing pipelines
and respond to changes in real time.
In the next lesson, you’ll see a demonstration using these combining operators in action.