Aside, Kotlin collection extensions might be my favorite thing I learned in the last two years.
>>> listOf(1, 2, 3).single { it > 2 } res0: kotlin.Int = 3 >>> listOf(1, 2, 3).single { it > 1 } java.lang.IllegalArgumentException: Collection contains more than one matching element. >>> listOf(1, 2, 3).single { it > 100 } java.util.NoSuchElementException: Collection contains no element matching the predicate.
>>> listOf(1, 2, 3).firstOrNull { it > 2 } res0: kotlin.Int? = 3 >>> listOf(1, 2, 3).firstOrNull { it > 1 } res1: kotlin.Int? = 2 >>> listOf(1, 2, 3).firstOrNull { it > 100 } res2: kotlin.Int? = null
Aside, Kotlin collection extensions might be my favorite thing I learned in the last two years.