Indizes suchen * Wo *

Da ist einindexWhere Funktion inVector das findet den Index einer Übereinstimmung.

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

Ich habe diese Funktion geschrieben, um zu findenalles Indizes, bei denen eine solche Übereinstimmung auftritt.

  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)
  }

Gibt es jedoch bereits eine eingebaute Funktion einer Sammlung?

Antworten auf die Frage(1)

Ihre Antwort auf die Frage