I don't understand how to pass a Coroutine where an Iterable is needed.
Assume I have the following function:
fun <T> iterate(iterable: Iterable<T>) {
for (obj in iterable) {
// do something..
}
}
I want to pass a coroutine:
iterate( ?? {
for (obj in objects) {
yield(transform(obj))
}
})
What am I supposed to put instead of the ??
for this to work? I tried buildIterator
and buildSequence
but neither one of them work.
You can use asIterable()
:
val seq = buildSequence {
for (i in 1..5) {
yield(i)
}
}.asIterable()
iterate(seq)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With