Kotlin: how to pass a sequence (coroutines) as Iterable <T>

I don't understand how to pass Coroutine where Iterable is required.

Suppose 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 should I use instead ?? for this? I tried buildIterator and buildSequence , but none of them work.

+5
source share
1 answer

You can use asIterable() :

 val seq = buildSequence { for (i in 1..5) { yield(i) } }.asIterable() iterate(seq) 
+6
source

Source: https://habr.com/ru/post/1274270/


All Articles