简体   繁体   English

Scala Currying函数与基本函数的解释

[英]Scala Currying Functions vs. Basic Functions Explained

What is the difference between the following two functions definition? 以下两个函数定义有什么区别? Please bear with me if this has been answered before. 如果以前已经回答过,请耐心等待。 I don't know what is the first type of function called to search for properly. 我不知道第一种被称为正确搜索的函数是什么。

def f1(a: String, b: T)(c: String, d: String, e: String = "_id"): Unit = ???

and

def f1(a: String, b: T, c: String, d: String, e: String = "_id"): Unit = ???

I know that it is similar to the question mentioned here . 我知道它类似于这里提到的问题。

  • Q1: What is the difference ? Q1:有什么区别?
  • Q2: What are the advantages of defining currying functions ? Q2:定义currying函数有什么好处?

A1 : A1

  • The difference is that in the latter you have to provide all the parameters at the same time. 不同之处在于后者必须同时提供所有参数。

A2 : A2

  • The big advantage (for me) is the ability to perform partial application of the function. 对我来说最大的好处是能够执行功能的部分应用

EDIT Advantages / Disadvantages concept-wise 编辑优势/劣势概念方面

lambda calculus semplicity: lambda calculus semplicity:

In theoretical computer science, currying provides a way to study functions with multiple arguments in very simple theoretical models such as the lambda calculus in which functions only take a single argument. 在理论计算机科学中,currying提供了一种在非常简单的理论模型中研究具有多个参数的函数的方法,例如lambda演算,其中函数仅采用单个参数。

(nice example here: https://en.wikipedia.org/wiki/Currying#Motivation ) (这里很好的例子: https//en.wikipedia.org/wiki/Currying#Motivation

Curried functions needs closure support: Curried函数需要closure支持:

Curried functions may be used in any language that supports closures Curried函数可以用于支持闭包的任何语言

Uncurried functions for performance : 未履行的绩效功能:

Uncurried functions are generally preferred for efficiency reasons, since the overhead of partial application and closure creation can then be avoided for most function calls. 出于效率原因,通常优选未计算的函数,因为对于大多数函数调用,可以避免部分应用和闭包创建的开销。

Curry–Howard correspondence: 库里 - 霍华德的通信:

The existence of currying and uncurrying is equivalent to the logical theorem currying和uncurrying的存在等同于逻辑定理 在此输入图像描述 as tuples (product type) corresponds to conjunction in logic, and function type corresponds to implication. 因为元组(产品类型)对应于逻辑中的连接,而函数类型对应于含义。

Source: https://en.wikipedia.org/wiki/Currying 资料来源: https//en.wikipedia.org/wiki/Currying

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM