is there some stdlib function to retrieve all the ...
# getting-started
e
is there some stdlib function to retrieve all the indices which satisfy a custom predicate?
j
Not directly (to my knowledge), but you could use
mapIndexedNotNull
like this:
Copy code
val indicesOfMatches = items.mapIndexedNotNull { i, elt -> i.takeIf { predicate(elt) }}
👍 1
e
pity, no
mapIndexedNotNull
on
FloatArray
..
k
You can do
myPrimitiveArray.asList().mapIndexedNotNull
. This avoids creating a whole
List
but has the overhead of boxing each element as it iterates through the array.
👍 1