When you hear the word “curry”, the very first thing that probably passes through your mind is a great part of Indian cuisine. Indian people use a very complex combination of spices to prepare the dish. They put all the ingredients in one by one to make a great curry.

当您听到“ 咖喱 ”一词时,可能首先想到的就是印度美食。 印度人使用非常复杂的香料组合来准备这道菜。 他们将所有食材一一放入咖喱中。

So the main trick is to put all the ingredients one by one. Similarly, in programming, currying is the technique of converting a function that takes multiple arguments into a function that takes one argument at a time and then returns a function.

因此,主要技巧是将所有成分一一放入。 类似地,在编程中, 柯里化是一种将接受多个参数的函数转换为一次接受一个参数然后返回一个函数的技术。

But in any programming language, we can easily declare a function that takes multiple arguments at a time, and most programmers are used to it. So why use currying?

但是,在任何一种编程语言中,我们都可以轻松地声明一个同时包含多个参数的函数,并且大多数程序员都习惯了该函数。 那为什么要用咖喱呢?

Besides making your food exceptional, it is used to allow the chaining of operations on a particular dataset. So instead of writing complex algorithms with some nested loops, you can accomplish the same with some simple commands.

除了让您的食物变得与众不同外,它还用于允许对特定数据集进行操作链接。 因此,您可以使用一些简单的命令来完成相同的操作,而不用编写带有嵌套循环的复杂算法。

It makes use of code reusability, and the less code you have to write, the fewer errors you’ll have! You can present currying / un-currying as a way to state the elimination and introduction of rules for and in constructive logic. This provides a connection to a more elegant motivation for why it exists.

它利用了代码的可重用性,并且您编写的代码越少,出错的次数就越少! 您可以呈现currying或uncurring,作为陈述消除和引入构造逻辑规则的一种方式。 这为它存在的原因提供了更为优雅的动机。

Programmers have the option to declare every function in two equivalent ways. In currying, a function takes just one argument and returns a function. Then the returned function takes one argument and returns the final result.

程序员可以选择以两种等效的方式声明每个函数。 在curring中,一个函数仅接受一个参数并返回一个函数。 然后,返回的函数将接受一个参数并返回最终结果。

So in every programmer’s mind, there may be one question: why would we take the more complicated direction, that is, first writing a function that returns a function, and then again calling the second function?

因此,在每个程序员的脑海中,可能会有一个问题:为什么我们要走更复杂的方向,即首先编写一个返回一个函数的函数,然后再次调用第二个函数?

Lets first look at the more popular style of defining a function:

首先让我们看一下定义函数的更流行的样式:

func multiply1(_ x: Int, _ y: Int) -> Int {return x*y
}

The above function takes two arguments and returns their multiplication result.

上面的函数接受两个参数并返回它们的相乘结果。

Now we can define the same function in a different way:

现在我们可以用不同的方式定义相同的函数:

func multiply2(_ x: Int) -> (Int) -> Int {return { $0 * x }
}

The difference between these two functions is in their calling style:

这两个函数之间的区别在于调用样式:

multiply1(3, 4) //returns 12
multiply2(3)(4) //returns 12

In the first function, we pass both the arguments at the same time. In the second function, we pass the first argument (which itself returns a function), and then we pass the second argument.

在第一个函数中,我们同时传递两个参数。 在第二个函数中,我们传递第一个参数(它本身返回一个函数),然后传递第二个参数。

Actually, both the functions are doing the same thing here. These two examples show how we can always transform a function that takes multiple arguments at a time into a series of functions that take one argument at a time. This is the process of currying. So multiply2 function is the curried version of multiply1.

实际上,这两个功能在这里都做相同的事情。 这两个示例说明了如何始终将每次使用多个参数的函数转换为一次使用一个参数的一系列函数。 这是欺骗的过程。 所以multiply2功能是multiply1的咖喱版本。

We can represent the second function as a chain of functions like this:

我们可以将第二个函数表示为一系列函数,如下所示:

//Benefit: 1
multiply2(3)(multiply2(4)(multiply2(5)(6))) //returns 360
//Benefit: 2
let multiplier = multiply2(2)
let integerList = 1...100
let x = integerList.map(multiplier) //returns [2, 4, 6, 8, 10, 12 ...]

These are some of the benefits of the curried function. You can always chain up the operations with some simple steps. Awesome, right?

这些是curried函数的一些好处。 您始终可以通过一些简单的步骤将操作链接起来。 太好了吧?

Let’s look at an another example:

让我们看另一个例子:

Now let’s create a MorningWalk for Sunday and add 100 steps into it.

现在,我们为星期日创建一个MorningWalk ,并向其中添加100个步骤。

So basically here we are calling the addSteps() instance method on the instance itself.

因此,基本上在这里,我们在实例本身上调用addSteps()实例方法。

But we can also do the same thing in the curried way like this:

但是我们也可以像下面这样用咖喱做同样的事情:

This is doing the same thing as we did above. First, we are assigning the addSteps and minusSteps methods into two different variables. Here at this stage, we are not calling any functions. We have just made references to the functions, the same as function pointers. In the next step, we are actually calling the functions that are stored inside stepIncreaser and stepDecreaser.

这和我们上面做的一样。 首先,我们将addStepsminusSteps方法分配给两个不同的变量。 在此阶段,这里我们没有调用任何函数。 我们刚刚引用了函数,与函数指针相同。 在下一步中,我们实际上将调用存储在stepIncreaserstepDecreaser中的函数

Now, stepIncreaser takes a single argument which is the MorningWalk instance, and returns a function whose type is (Int) -> (). So the returned function takes an argument of type Int and returns nothing. Here, the returned function and addSteps() function have the same type of method signature. The same concept applies to the stepDecreaser.

现在, stepIncreaser接受一个参数,即MorningWalk实例,并返回一个类型为(Int)-> ()的函数。 因此,返回的函数采用Int类型的参数,但不返回任何内容。 在此,返回的函数和addStep s()函数具有相同类型的方法签名。 同样的概念也适用于stepDecrea ser。

So, at last, we can say that an instance method in Swift is also a type method. It takes an instance of the class as an argument and returns a function, which will then take other arguments and return/update the final result.

因此,最后我们可以说Swift中的实例方法也是一种类型方法。 它以类的实例作为参数并返回一个函数,然后该函数将采用其他参数并返回/更新最终结果。

We can call the above methods like this also:

我们也可以像这样调用上述方法:

结论 (Conclusion)

In this article, we had a function with more than one argument. We transformed it into a function which always took a single argument at a time, resulting in another function, until there was no argument left. This then gave us the final result. So we can say that functions are nothing more than ordinary values which can be produced and returned by other functions.

在本文中,我们有一个带有多个参数的函数。 我们将其转换为一个函数,该函数始终一次仅使用一个参数,从而生成另一个函数,直到没有参数为止。 然后,这给了我们最终结果。 因此,我们可以说函数不过是其他函数可以生成和返回的普通值。

Now you have something new and interesting to try out on your daily programming tasks!

现在,您可以尝试一些新的有趣的日常编程任务!

翻译自: https://www.freecodecamp.org/news/an-introduction-to-swifts-curried-function-e4b55d10a506/

Swift中的函数curring简介相关推荐

  1. Py之Numpy:Numpy库中常用函数的简介、应用之详细攻略

    Py之Numpy:Numpy库中常用函数的简介.应用之详细攻略 目录 Numpy库中常用函数的简介.应用 1.X, Y = np.meshgrid(X, Y) 相关文章 Py之Numpy:Numpy库 ...

  2. Matlab中 intlinprog函数用法简介

    Matlab中 intlinprog函数用法简介 本来想要自己亲手写一遍的,发现了一优质博文基本上跟我做过的例题大差不差,所以就直接放上链接. 参考链接 https://www.cnblogs.com ...

  3. 数模--0-1规划问题~Matlab中 intlinprog函数用法简介

    1. intlinprog介绍 intlinprog是matlab中用于求解混合整数线性规划(Mixed-integer linear programming)的一个函数,用法基本和linprog差不 ...

  4. oracle中CAST函数使用简介

    CAST()函数可以进行数据类型的转换. CAST()函数的参数有两部分,源值和目标数据类型,中间用AS关键字分隔. 以下例子均通过本人测试. 一.转换列或值 语法:cast( 列名/值 as 数据类 ...

  5. oracle mysql substr_Oracle数据库中substr()函数简介说明

    摘要: 下文讲述Oracle数据库中substr函数应用简介,如下所示: oracle substr函数功能说明 substr函数功能: substr函数在oracle数据库中的功能为:字符串截取函数 ...

  6. python反余弦函数_Python代码中acos()函数有什么功能呢?

    摘要: 下文讲述Python代码中acos()函数的简介说明,如下所示: acos()函数功能 用于计算出x的反余弦弧度值 acos()函数语法 math.acos(x) ---------参数说明- ...

  7. Swift中方法的多面性

    虽然 Objective-C 的语法相对于其他编程语言来说写法有点奇怪,但是当你真正使用的时候它的语法还是相当的简单.下面有一些例子: + (void)mySimpleMethod {// 类方法// ...

  8. python中seed的用法什么作用_Python代码中seed()函数有什么功能呢?

    摘要: 下文讲述Python代码中seed()函数的简介说明,如下所示: seed()函数功能 用于改变随机数生成器的种子, 常用于其它随机函数之前运行此函数 seed()函数语法 seed.seed ...

  9. python中cos函数_Python代码中cos()函数有什么功能呢?

    摘要: 下文讲述Python代码中cos()函数的简介说明,如下所示: cos()函数功能 用于计算出x 弧度-所对应的的余弦值 cos()函数语法 math.cos(x) ---------参数说明 ...

最新文章

  1. jsfl 改变舞台宽高
  2. filebeat相关registry文件内容解析
  3. NOP (code)_NOP指令作用及解析
  4. 《Genesis-3D游戏引擎系列教程-入门篇》五:脚本
  5. Vue.js前后端分离2
  6. php论坛如何加评论表情,DEDECMS会员评论时添加多个表情
  7. 791. 高精度加法
  8. 华为NP课程笔记10-BGP案例讲解
  9. Expo大作战(四十一)【完】--expo sdk 之 Assets,BarCodeScanner,AppLoading
  10. 未来6英寸主战场,碳化硅-精密划片机
  11. ora-22858:数据类型的变更无效 for clod
  12. 高斯投影法正反算代码MATLAB版本
  13. 性能测试方法及基本流程
  14. android自动识别文字,Android文字识别tesseract ocr -训练样本库 识别字库
  15. c# wifi串口通信_C#中的串口通信
  16. 数据分析入门学习指南,零基础小白都能轻松看懂
  17. 百度地图、高德地图和腾讯地图定位不准确的解决方案
  18. vcs 覆盖率收集2——覆盖率选项 + 合并覆盖率
  19. sqlite3数据库的使用及其对应的API函数接口的使用
  20. 重磅!新职业技能证书来了,让你高薪就业

热门文章

  1. 自动增长 mysql
  2. dbhelper的使用
  3. 1111 复习 形状的打印
  4. SQLServer2012x64数据库 安装过程 imp
  5. django-模板渲染上下文context-0223
  6. 自动化测试selenum
  7. jquery-模态框的显示与消失操作
  8. mysql数据库创建带-的数据库名
  9. HDFS 入门和基本操作
  10. ipvs,ipvsadm的安装及使用