I have seen this usage of Function.tupled example in another answer: Map(1 -> "one", 2 -> "two") map Function.tupled(_ -> _.length)
.
It works:
scala> Map(1 -> "one", 2 -> "two") map Function.tupled(_ -> _.length)
<console>:5: warning: method tupled in object Function is deprecated:
Use `f.tuple` instead
Map(1 -> "one", 2 -> "two") map Function.tupled(_ -> _.length)
^
res0: scala.collection.immutable.Map[Int,Int] = Map(1 -> 3, 2 -> 3)
It seems I can do without if I don't want to use the placeholder syntax.
scala> Map(1 -> "one", 2 -> "two") map (x => x._1 -> x._2.length)
res1: scala.collection.immutable.Map[Int,Int] = Map(1 -> 3, 2 -> 3)
The direct use of the placeholder syntax does not work:
scala> Map(1 -> "one", 2 -> "two") map (_._1 -> _._2.length)
<console>:5: error: wrong number of parameters; expected = 1
Map(1 -> "one", 2 -> "two") map (_._1 -> _._2.length)
How does Function.tupled work? There seem to be a lot happening in Function.tupled(_ -> _.length)
. Also how would I use it to not get the deprecation warning?