Julio Zynger
03/04/2020, 12:17 PMmyCollection.indexOf(myCollection.find { predicate(it) })
(plus avoid the handling of -1
as a result type.Julio Zynger
03/04/2020, 12:17 PMSackCastellon
03/04/2020, 12:22 PMindexOfFirst
.
See: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/index-of-first.htmlSackCastellon
03/04/2020, 12:23 PM-1
if no element matches the predicateJulio Zynger
03/04/2020, 12:25 PMval index: Int = myCollection.theMagicFunctionHere { myPredicate(it) } ?: somethingElse()
spand
03/04/2020, 12:25 PMDias
03/04/2020, 12:27 PMDias
03/04/2020, 12:28 PMSackCastellon
03/04/2020, 12:29 PMtakeIf { it != -1 }
to transform a -1
to null
Or create an extension function like:
inline fun <T> Iterable<T>.indexOfFirstOrNull(predicate: (T) -> Boolean): Int? = indexOfFirst(predicate).takeIf { it != -1 }
Julio Zynger
03/04/2020, 12:31 PM