There\'s a indexWhere
function in Vector
that finds the index of a match.
def indexWhere(p: (A) ⇒ Boolean, from: Int): Int
> Fin
zipWithIndex
, filter
, and map
are built-ins that can be combined to get all the indices of some predicate.
Get the indices of the even values in the list.
scala> List(1,2,3,4,5,6,7,8,9,10).zipWithIndex.filter(_._1 % 2 == 0).map(_._2)
res0: List[Int] = List(1, 3, 5, 7, 9)
You can also use collect
as @0__ notes.
scala> List(1,2,3,4,5,6,7,8,9,10).zipWithIndex.collect{ case(a,b) if a % 2 == 0 => b}
res1: List[Int] = List(1, 3, 5, 7, 9)