There's a indexWhere
function in Vector
that finds the index of a match.
def indexWhere(p: (A) ⇒ Boolean, from: Int): Int
> Finds index of the first element satisfying some predicate after or
> at some start index.
http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Vector
I wrote this function to find all indexes where such a match occurs.
def getAllIndexesWhere[A,B](as: List[A])(f: (B => Boolean))(g: A => B): Vector[B] = {
def go(y: List[A], acc: List[Option[B]]): Vector[B] = as match {
case x :: xs => val result = if (f(g(x))) Some(g(x)) else None
go(xs, acc :+ result)
case Nil => acc.flatten.toVector
}
go(as, Nil)
}
However, is there already a built-in function of a collection?