Scala: normal functions vs tupled functions?

淺唱寂寞╮ 提交于 2021-02-08 14:11:38

问题


What's the difference between these? I know that their type signatures are different, and that all functions start off normal and have to be .tupled to get their tupled form. What's the advantage of using un-tupled (but non-curried) functions? Especially because it seems to me that passing multiple arguments to a tupled function automagically unpacks them anyway, so by all appearances they are the same.

One difference i see is that it forces you to have types for every number of function arguments: Function0, Function1, Function2, Function3 etc, whereas tupled functions are all just Function1[A, R], but that seems like a downside. What's the big advantage of using non-tupled functions that they're the default?


回答1:


Tupled functions require that a tuple object be created when they are called (unless the arguments happen to already be packed into a tuple). Non-tupled functions simply define a method that takes the appropriate number of arguments. Thus, given the JVM architecture, non-tupled functions are more efficient.




回答2:


Consider this example:

scala> def mult = (x: Int, y: Int) => x * y
mult: (Int, Int) => Int

scala> val list = List(1, 2, 3)
list: List[Int] = List(1, 2, 3)

scala> list zip list map mult
<console>:10: error: type mismatch;
 found   : (Int, Int) => Int
 required: ((Int, Int)) => ?
              list zip list map mult
                                ^

scala> list zip list map mult.tupled
res4: List[Int] = List(1, 4, 9)

There are many situations where you end up pairing elements in tuples. In such situations, you need a tupled function to handle it. But there are many other places where that is not true! For example:

scala> list.foldLeft(1)(mult)
res5: Int = 6

scala> list.foldLeft(1)(mult.tupled)
<console>:10: error: type mismatch;
 found   : ((Int, Int)) => Int
 required: (Int, Int) => Int
              list.foldLeft(1)(mult.tupled)
                                    ^

So, basically, Scala has a dichotomy between tuples and parameters, which means you have to convert functions from tupled to untupled and vice versa here and there.



来源:https://stackoverflow.com/questions/8307498/scala-normal-functions-vs-tupled-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!