Inspired by danielnixon's answer, a reusable version (Scala 3):
extension [T](ts: Iterable[T]) {
def indexBy[K](f: T => K): Map[K, T] =
ts.map(t => f(t) -> t).toMap
}
val byAge = stooges.indexBy(_.age)
Also, I could swear from my Scala 2 use a few years ago that Scala includes the above somewhere in the standard library (in IterableOps or the like), but I can't find it. Could it have been removed in Scala 3?