Context: ```data class Test(val num: Int) { fu...
# announcements
k
Context:
Copy code
data class Test(val num: Int) {
    fun other(): Int {
        println("hit")
        return num + 5
    }
}
Is there an elegant way to achieve something like
list.first { it.other() > 5 }.other()
, but without having to call
other
twice?
list.map { it.other() }.first { it > 5 }
is also not really what I want, I basically want the minimum possible of
other
calls.
d
you probably can do something with a
fold
m
You can use a sequence:
list.asSequence().map { it.other() }.first { it > 5 }
k
How could I forget about those, thanks!
I was surprised when InteliJ suggested a function
firstNotNullResult
while typing something unrelated, turns out I'm not the only one that wants this: https://github.com/cbeust/klaxon/blob/master/src/main/kotlin/com/beust/klaxon/internal/Extensions.kt