bbaldino
08/26/2020, 9:26 PM.any { it > bigEnoughNum }
first, but then if that returns false I have to iterate again to find the actual max, which doesn't seem efficient.nanodeath
08/26/2020, 9:27 PMiterable.first { it > bigEnoughNum }
?bbaldino
08/26/2020, 9:28 PMany
method? If it fails I'd still have to iterate over the whole list to find the max.nanodeath
08/26/2020, 9:28 PMnanodeath
08/26/2020, 9:29 PMfirstOrNull
or find
the same exact waybbaldino
08/26/2020, 9:29 PM> bigEnoughNum
ephemient
08/26/2020, 9:30 PMiterable.maxByOrNull {
if (it > bigEnoughNum) return
it
}
will break earlybbaldino
08/26/2020, 9:31 PMnanodeath
08/26/2020, 9:32 PMfirst
? I'm still unclear why that doesn't workNir
08/26/2020, 9:32 PMNir
08/26/2020, 9:32 PMNir
08/26/2020, 9:32 PMNir
08/26/2020, 9:38 PMNir
08/26/2020, 9:38 PMNir
08/26/2020, 9:39 PMval x = listOf(1,10,11)
println(x.maxByOrNull { if (it > 8) return@maxByOrNull it else it })
Nir
08/26/2020, 9:39 PMbbaldino
08/26/2020, 9:40 PMreturn@maxByOrNull
is the same as the normal return that happens therebbaldino
08/26/2020, 9:40 PMNir
08/26/2020, 9:43 PMprintln(run { x.maxByOrNull { if (it > 8) return@run it else it }!! })
Nir
08/26/2020, 9:43 PMNir
08/26/2020, 9:44 PMNir
08/26/2020, 9:45 PMNir
08/26/2020, 9:45 PMNir
08/26/2020, 9:45 PMbbaldino
08/26/2020, 9:46 PMNir
08/26/2020, 9:48 PMfun <T: Comparable<T>> Iterable<T>.maxOrOver(threshold: T) = this.maxByOrNull { if (it > threshold) return it else it}!!
ephemient
08/26/2020, 10:07 PMinline fun <T: Comparable<T>> Iterable<T>.firstOrMaxOrNull(
crossinline predicate: (T) -> Boolean
): T? = maxByOrNull { if (predicate(it)) return it else it }
not sure how general it should be, though. seems pretty specializedstreetsofboston
08/26/2020, 10:15 PMbbaldino
08/26/2020, 10:16 PMbbaldino
08/26/2020, 10:18 PMephemient
08/26/2020, 10:22 PMbbaldino
08/26/2020, 10:28 PMNir
08/26/2020, 10:38 PMNir
08/26/2020, 10:38 PMNir
08/26/2020, 10:38 PMNir
08/26/2020, 10:39 PMephemient
08/27/2020, 12:09 AMephemient
08/27/2020, 12:10 AMephemient
08/27/2020, 12:10 AMephemient
08/27/2020, 12:11 AMNir
08/27/2020, 12:22 AMNir
08/27/2020, 12:23 AMNir
08/27/2020, 12:24 AMNir
08/27/2020, 12:24 AMmap
which are implemented for both (but with the differences you mentioned)Nir
08/27/2020, 12:25 AM