How best to call suspend functions from `Sequence....
# coroutines
r
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
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
Thanks @Jonathan!
d
Maybe contracts can be used to capture the continuation from within lambdas.
j
@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
Right, the static extension functions of
Sequence
can't make any guarantees about where/when the programmer invokes a terminal operation.