Scala:普通函数与元组函数? [英] Scala: normal functions vs tupled functions?

查看:46
本文介绍了Scala:普通函数与元组函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些之间有什么区别?我知道它们的类型签名是不同的,并且所有功能都从正常开始,必须使用 .tupled 来获取其元组形式.使用非组合式(但非咖喱式)功能的优点是什么?特别是因为在我看来,将多个参数传递给元组函数无论如何都会自动解压缩它们,因此从外观上看,它们都是相同的.

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.

我看到的一个区别是,它迫使您必须为每种函数参数拥有类型: Function0 Function1 Function2 Function3 等,而元组函数都只是 Function1 [A,R] ,但这似乎是不利的一面.使用非组合函数作为默认函数的最大优势是什么?

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?

推荐答案

混合函数要求在调用它们时创建一个元组对象(除非参数已被打包到一个元组中).非组合函数仅定义一个采用适当数量的参数的方法.因此,在使用JVM体系结构的情况下,非组合函数将更加有效.

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.

这篇关于Scala:普通函数与元组函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆