79283632

Date: 2024-12-16 04:05:31
Score: 0.5
Natty:
Report link

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?

Reasons:
  • Has code block (-0.5):
  • Ends in question mark (2):
  • High reputation (-1):
Posted by: Jake