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

rocketraman

10/10/2018, 12:54 PM
How best to call suspend functions from
Sequence.map
and
flatMap
? These operations on
Sequence
are not inlined, unlike
List
, so you get the error
Suspension functions can only be called within coroutine body
. I don't want to use
runBlocking
inside the lambda.
j

Jonathan

10/10/2018, 12:55 PM
Use channel instead of sequence.
Of course it has its limitation, and you may want to read: https://github.com/Kotlin/kotlinx.coroutines/issues/254
r

rocketraman

10/10/2018, 12:58 PM
Thanks @Jonathan!
d

Dico

10/10/2018, 1:51 PM
Maybe contracts can be used to capture the continuation from within lambdas.
j

Jonathan

10/10/2018, 2:40 PM
@Dico. No lambda is not executed in place. The problem is that we don't know on which context the lambda of
Sequence.map
will be executed (or if it will be executed at all). This is inherent to sequences since everything will be evaluated lazily on the context of iterator's usage.
d

Dico

10/10/2018, 2:57 PM
Right, the static extension functions of
Sequence
can't make any guarantees about where/when the programmer invokes a terminal operation.
3 Views