I have this function where I'm counting down dates...
# coroutines
p
I have this function where I'm counting down dates. Now line 5 is suspending. The compiler warns me that it can only be called from within a coroutine body
d
I think you want to use channels in this case:
Copy code
fun main(args: Array<String>) {
    runBlocking {
        produceDates()
            .mapNotNull { it /* something suspending */ }
            .firstOrNull()
    }
}

fun CoroutineScope.produceDates(): ReceiveChannel<LocalDate> = produce {
    generateSequence(LocalDate.now()) { it.minusDays(1) }
        .take(10)
        .forEach { send(it) }
}
But something better seems to be planned: https://github.com/Kotlin/kotlinx.coroutines/issues/254
g
Sequences is completely syncronous API, so run blocking is the only option.
Channels is async sequence of events in kotlinx.coroutines, but big difference: channel is hot stream, so next event computed before it consumed, so as Take already mentioned, real solution is cold streams abstraction
p
Is there any progress on that issue? It seems that it's top priority (DontUseThisAnnotations everywhere) but I can't find any commits tackling that issue.
b
It's just marked experimental because it is going to change
g
It seems that it’s top priority (DontUseThisAnnotations everywhere
this is not related to cold streams directly. Channels will be there even after cold streams release. This is just means that API of channels experimental, may be changed, so you have to use annotations or enable it for your project explictly