1 /* <lambda>null2 * 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 flow.md by Knit tool. Do not edit. 6 package kotlinx.coroutines.guide.exampleFlow15 7 8 import kotlinx.coroutines.* 9 import kotlinx.coroutines.flow.* 10 11 fun log(msg: String) = println("[${Thread.currentThread().name}] $msg") 12 13 fun simple(): Flow<Int> = flow { 14 for (i in 1..3) { 15 Thread.sleep(100) // pretend we are computing it in CPU-consuming way 16 log("Emitting $i") 17 emit(i) // emit next value 18 } 19 }.flowOn(Dispatchers.Default) // RIGHT way to change context for CPU-consuming code in flow builder 20 <lambda>null21fun main() = runBlocking<Unit> { 22 simple().collect { value -> 23 log("Collected $value") 24 } 25 } 26