I'd like to return the first non-null value after the transformation of a list of elements in Kotlin, so something like this:
suspend fun myFunction(): Any {
val firstNonNullId = this.mapNotNull{ it.id }
?.first{ transform(id) != null }
return transform(firstNonNullID)
}
What would be a better and more idiomatic way to write this function? I'd like to call upon transform(it) as few times as possible, and the transformation in question is also a suspend function.
Edit: The asSequence() solution gives an error when the transform() is a suspend function, as it must be called from a coroutine body. This happens even if the overall myFunction() is a suspend function. What should the solution be when the transformation is a suspend function?