A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That // ... } def zipWithIndexIf[T](list: List[T])(pred: T => Boolean): List[(T, Option[Int])] = { type R = List[(T, Option[Int])] def doZip(zipped: R, left: List[T], index: Int): R = left match { case x :: xs if pred(x) => doZip((x, Some(index)) :: zipped, xs, index + 1) case x :: xs => doZip((x, None) :: zipped, xs, index) case Nil => zipped } doZip(Nil, list, 0).reverse }