I have a two dimension collection(say Vector[Vector[Int]]
) and I want to find the index of a element in it. My solution is like:
def find(vec: Vector[Vector[Int]], target: Int) = {
def indexOfTarget(v: Vector[Int]) = v.indexOf(target)
val r = vec.indexWhere((v) => indexOfTarget(v) != -1)
val c = indexOfTarget(vec(r))
(r, c)
}
But it's just... ugly. And it invokes indexOfTarget
one more time than necessary.
Is there a better way?