https://kotlinlang.org logo
Title
i

igor.wojda

04/29/2020, 8:18 AM
suspend fun sleepSort(list: List<Int>): List<Int> {

    val listInt = listOf(1, 2, 3)
    val listDeferred = list.map {
        GlobalScope.async {
            println("start $it")
        }
    }
    listDeferred.first().await()

    return listOf()
}
The above code gives me this result
start 1
start 2
start 3
I wonder why all deferred jobs are launched instead of jus the first one 🤔
o

octylFractal

04/29/2020, 8:19 AM
list.map
is not lazy -- it returns a List itself
if you want a lazily evaluated mapping, use
.asSequence()
first
i

igor.wojda

04/29/2020, 8:20 AM
ahh, right, thx
a

araqnid

04/29/2020, 9:21 AM
or specify CoroutineStart.LAZY on async
👍 1