Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function.tupled and placeholder syntax

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?

like image 410
huynhjl Avatar asked Mar 01 '10 06:03

huynhjl


1 Answers

UPDATE The deprecation was removed today, in response to this problem.


Tupling a function is simply adapting FunctionN[A1, A2, ..., AN, R] to a Function1[(A1, A2, ..., AN), R]

Function.tuple is deprecated in favour of FunctionN#tupled. A (possibly unintended) consequence of this is that the type inferencer can't infer the parameter types in:

scala> Map(1 -> "one", 2 -> "two") map (_ -> _.length).tupled                 
<console>:5: error: missing parameter type for expanded function ((x$1, x$2) => x$1.$minus$greater(x$2.length))
       Map(1 -> "one", 2 -> "two") map (_ -> _.length).tupled
                                        ^
<console>:5: error: missing parameter type for expanded function ((x$1: <error>, x$2) => x$1.$minus$greater(x$2.length))
       Map(1 -> "one", 2 -> "two") map (_ -> _.length).tupled

Any of these will work:

scala> Map(1 -> "one", 2 -> "two") map { case (a, b)  => a -> b.length }
res8: scala.collection.immutable.Map[Int,Int] = Map(1 -> 3, 2 -> 3)

scala> Map(1 -> "one", 2 -> "two") map ((_: Int) -> (_: String).length).tupled           
res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 3, 2 -> 3)

scala> Map(1 -> "one", 2 -> "two") map ((p: (Int, String))  => p._1 -> p._2.length)
res12: scala.collection.immutable.Map[Int,Int] = Map(1 -> 3, 2 -> 3)

I suggest you read the answers to this recent question to get a deeper understanding of '_' in function literals, and how type inferencing works:

In Scala, what is the difference between using the `_` and using a named identifier?

UPDATE

In answer to the comment, it does.

scala> val f = (x:Int, y:String) => x + ": " + y
f: (Int, String) => java.lang.String = <function2>

scala> f.tupled
res0: ((Int, String)) => java.lang.String = <function1>

scala> Map(1 -> "1") map f.tupled
res1: scala.collection.immutable.Iterable[java.lang.String] = List(1: 1)

This requires Scala 2.8. Note that Map#map can result in another map, if the return type of the function is a Tuple2, otherwise a List, as above.

like image 80
retronym Avatar answered Oct 02 '22 17:10

retronym