Я предлагаю:
def square(x:Int) = x*x
def associate[A, B](fun: A => B, list: List[A]): List[(A, B)] = {
val distinct: List[A] = list.distinct
distinct.zip(distinct.map(fun).foldLeft(List.empty[Option[B]])((maybeOuts, out) => {
maybeOuts :+ distinct.collectFirst { case `out` => out }
}))
.collect { case (in, Some(out)) => in -> out }
}
println(associate(square, List(2, 4, 16, 5, 10, 100, 105)))
println(associate((_: Double).toInt, List[Double](2, 4, 16, 5, 10, 100, 105)))
println(associate(identity[Int], List(2, 4, 16, 5, 10, 100, 105)))
println(associate[Int, Double](Math.pow(_, 2), List(2, 4, 16, 5, 10, 100, 105)))
который печатает
Список ((2,4), (4,16), (10,100))
Список ((2.0,2), (4.0,4), (16,0,16), (5,0,5), (10,0,10), (100,0100), (105,0,105))
Список ((2,2), (4,4), (16,16), (5,5), (10,10), (100,100), (105,105))
Список ((2,4,0), (4,16,0), (10,100,0))
Надеюсь, это поможет.