Nat Strangerweather
06/11/2022, 7:13 PMephemient
06/11/2022, 7:19 PMlist.indices.filter { !list[it] }
Nat Strangerweather
06/11/2022, 7:20 PM.indexOf()
but was getting nowhere.Starr
06/11/2022, 7:20 PMfilterIndexed
existsephemient
06/11/2022, 7:25 PMlist.mapIndexedNotNull { i, bool ->
if (bool) null else i
}
would work but is not necessary when first one sufficesKlitos Kyriacou
06/12/2022, 10:38 AMlist.withIndex().filter { !it.value }.map { it.index }
This may be more performant than ephemient's first solution if the list is large and not random-access.Nat Strangerweather
06/12/2022, 10:43 AMKlitos Kyriacou
06/12/2022, 10:46 AMNat Strangerweather
06/12/2022, 10:48 AMKlitos Kyriacou
06/12/2022, 10:53 AMArrayList
and the type of list you get by calling listOf()
. Examples of non-random-access lists include LinkedList
and generally any list where finding the value of list[n]
requires you to start from index 0 and iterate the list through each element until you get to element n.Nat Strangerweather
06/12/2022, 10:58 AMephemient
06/12/2022, 8:09 PMAnouar di Kali
06/12/2022, 9:10 PMval list:List<Boolean> =BooleanArray(100){Random.nextBoolean()}.toList()
val start: Long = System.nanoTime()
list.mapIndexedNotNull { i, bool ->
if (bool) null else i
}
val end: Long = System.nanoTime();
println(end - start)
ephemient
06/12/2022, 10:26 PMAnouar di Kali
06/12/2022, 10:41 PMnkiesel
06/13/2022, 6:55 PMmapIndexedNotNull
approach is that you do not need a named list. Of course, this does not matter if you anyway wrap it into a function.