1 // This file was automatically generated from channels.md by Knit tool. Do not edit.
2 package kotlinx.coroutines.guide.exampleChannel04
3 
4 import kotlinx.coroutines.*
5 import kotlinx.coroutines.channels.*
6 
<lambda>null7 fun main() = runBlocking {
8     val numbers = produceNumbers() // produces integers from 1 and on
9     val squares = square(numbers) // squares integers
10     repeat(5) {
11         println(squares.receive()) // print first five
12     }
13     println("Done!") // we are done
14     coroutineContext.cancelChildren() // cancel children coroutines
15 }
16 
<lambda>null17 fun CoroutineScope.produceNumbers() = produce<Int> {
18     var x = 1
19     while (true) send(x++) // infinite stream of integers starting from 1
20 }
21 
<lambda>null22 fun CoroutineScope.square(numbers: ReceiveChannel<Int>): ReceiveChannel<Int> = produce {
23     for (x in numbers) send(x * x)
24 }
25