1 /* 2 * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 // This file was automatically generated from channels.md by Knit tool. Do not edit. 6 package kotlinx.coroutines.guide.exampleChannel04 7 8 import kotlinx.coroutines.* 9 import kotlinx.coroutines.channels.* 10 <lambda>null11fun main() = runBlocking { 12 val numbers = produceNumbers() // produces integers from 1 and on 13 val squares = square(numbers) // squares integers 14 repeat(5) { 15 println(squares.receive()) // print first five 16 } 17 println("Done!") // we are done 18 coroutineContext.cancelChildren() // cancel children coroutines 19 } 20 <lambda>null21fun CoroutineScope.produceNumbers() = produce<Int> { 22 var x = 1 23 while (true) send(x++) // infinite stream of integers starting from 1 24 } 25 <lambda>null26fun CoroutineScope.square(numbers: ReceiveChannel<Int>): ReceiveChannel<Int> = produce { 27 for (x in numbers) send(x * x) 28 } 29