https://kotlinlang.org logo
#coroutines
Title
# coroutines
k

karelpeeters

11/05/2018, 10:06 PM
Well this works, feels bad though:
Copy code
private val loop = iterator {
    for (i in 0 until 10) {
        println(i)
        yield(Unit)
    }
}

fun doIteration() {
    loop.next()
}
n

nwh

11/05/2018, 10:08 PM
This is part of the standard library, not coroutines. Try asking in #general. Also please use threads on Slack so questions don't get buried!
k

karelpeeters

11/05/2018, 10:09 PM
I'm specifically asking about how to do this myself with coroutines.
n

nwh

11/05/2018, 10:11 PM
Do you need it to be suspending? And if so where is the point of suspension? I see your yield call
k

karelpeeters

11/05/2018, 10:13 PM
The point of suspending is that the loop literally needs to suspend for
doIteration
to return.
n

nwh

11/05/2018, 10:19 PM
No, I understand that, I mean where do you want it to suspend. Anyway, maybe this will help: https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/channels.md#building-channel-producers I believe you can simply consume one at a time, which seems to meet your needs, although it would take some refactoring.
d

Dico

11/05/2018, 10:34 PM
In your specific example, you might be able to use a dispatcher that stores the continuation for the next call.
k

karelpeeters

11/05/2018, 10:40 PM
@nwh That seems to be what I was looking for, thanks!
👍🏻 1
e

elizarov

11/06/2018, 4:56 AM
The design document provides explanations and examples on how to write things like that https://github.com/Kotlin/KEEP/blob/master/proposals/coroutines.md
3 Views