// WITH_COROUTINES // WITH_STDLIB // FILE: test.kt interface Flow { abstract suspend fun collect(collector: FlowCollector) } interface FlowCollector { suspend fun emit(value: T) } inline fun flow(crossinline body: suspend FlowCollector.() -> Unit): Flow = object : Flow { override suspend fun collect(collector: FlowCollector) = collector.body() } suspend inline fun Flow.collect(crossinline body: suspend (T) -> Unit) = collect(object : FlowCollector { override suspend fun emit(value: T) = body(value) }) inline fun Flow.filter(crossinline predicate: suspend (T) -> Boolean): Flow = flow { this@filter.collect { if (predicate(it)) emit(it) } } inline fun Flow<*>.filterIsInstance(): Flow = filter { it is R } as Flow // FILE: box.kt import kotlin.coroutines.* import helpers.* fun builder(c: suspend () -> Unit) { c.startCoroutine(EmptyContinuation) } fun box(): String { var result = "fail" builder { flow { emit("OK") }.filterIsInstance().collect { result = it } } return result }