Transforming Operators

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this lesson, you’ll explore transforming operators. These operators let you manipulate and reshape the data flowing through your streams. You’ll cover the following operators:

  • map: This operator transforms each item into a flow. It’s similar to the map function in other collections, letting you apply a function to each element to create a new flow.
  • flatMapLatest: This operator allows you to create a new flow from each item in an existing flow. It’s useful when you need to work with asynchronous tasks or nested data structures.
  • transform: This operator allows you to perform various operations like emitting multiple values, skipping emissions, or applying more complex logic on each item in the flow.

The map Operator

Suppose you’re processing a stream of carrots in your factory, and you need to convert them into carrot juice. You could use the map operator to transform each carrot into a juice object:

fun carrotJuice(): Flow<CarrotJuice> = carrots.asFlow()
  .map { carrot -> carrotJuicer.toJuice(carrot) }
  .collect { juice -> println("Produced: $juice") }

The flatMapLatest Operator

Next, suppose you have a collection of carrot batches, and each batch has some number of carrots. You process each carrot from the batch, but if a new batch arrives, you switch to that one:

fun processedCarrots(): Flow<Carrot> = batches.asFlow()
  .flatMapLatest { batch -> batch.carrots.asFlow() }
  .map { carrot -> processCarrot() }
  .collect { carrot -> println("Processed $carrot") }

The transform Operator

Finally, imagine you have a flow of carrots, and you need to check their quality. You only want to emit the high-quality ones for further processing.

fun processedCarrots(): Flow<Carrot> = carrots.asFlow()
  .transform { carrot ->
    if (carrot.isHighQuality) {
      emit(carrot) // Emit only high-quality carrots
      println("High-quality carrot detected: ${carrot.id}")
    } else {
      println("Discarding low-quality carrot: ${carrot.id}")
    }
  }
  .collect { carrot ->
    println("Processing carrot: ${carrot.id}")
  }

Wrap-Up

In this lesson, you’ve explored key transforming operators in Kotlin Flow, including map, flatMapLatest, and transform. You’ve seen how these operators can be used to manipulate and transform data as it flows through your reactive streams.

See forum comments
Download course materials from Github
Previous: Introduction Next: Transforming Operators Demo