I'm reading this but I think it's lacking an examp...
# coroutines
c
I'm reading this but I think it's lacking an example, so I have to imagine it myself... https://kotlinlang.org/docs/reference/coroutines/channels.html#building-channel-producers
The pattern where a coroutine is producing a sequence of elements is quite common. This is a part of producer-consumer pattern that is often found in concurrent code. You could abstract such a producer into a function that takes channel as its parameter, but this goes contrary to common sense that results must be returned from functions.
It means something like this?
Copy code
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*

suspend fun produceSquares(channel: Channel<Int>) {
    for (x in 1..5) channel.send(x * x)
    channel.close()
}

fun main() = runBlocking {
    val squares: Channel<Int> = Channel()
    launch {
        produceSquares(squares)
    }

    for (value in squares) {
        println(value)
    }
    println("Done!")
}