我在一些文章和博客中看到了curry函数的引用,但我找不到一个好的解释(或者至少一个有意义的解释!)
当前回答
curry的一个例子是当你有一个函数时,你现在只知道其中一个参数:
例如:
func aFunction(str: String) {
let callback = callback(str) // signature now is `NSData -> ()`
performAsyncRequest(callback)
}
func callback(str: String, data: NSData) {
// Callback code
}
func performAsyncRequest(callback: NSData -> ()) {
// Async code that will call callback with NSData as parameter
}
在这里,因为你不知道回调的第二个参数,当它发送给performAsyncRequest(_:)时,你必须创建另一个lambda /闭包来发送给函数。
其他回答
下面是JavaScript中的一个curry示例,这里的multiply返回用于将x乘以2的函数。
const multiply = (presetConstant) => {
return (x) => {
return presetConstant * x;
};
};
const multiplyByTwo = multiply(2);
// now multiplyByTwo is like below function & due to closure property in JavaScript it will always be able to access 'presetConstant' value
// const multiplyByTwo = (x) => {
// return presetConstant * x;
// };
console.log(`multiplyByTwo(8) : ${multiplyByTwo(8)}`);
输出
multiplyByTwo(8): 16
有一个“咖喱在理性ml”的例子。
let run = () => {
Js.log("Curryed function: ");
let sum = (x, y) => x + y;
Printf.printf("sum(2, 3) : %d\n", sum(2, 3));
let per2 = sum(2);
Printf.printf("per2(3) : %d\n", per2(3));
};
curry是Java Script的高阶函数之一。
curry是一个包含许多参数的函数,它会被重写,这样它会接受第一个参数并返回一个函数,而这个函数会使用剩余的参数并返回值。
困惑吗?
让我们看一个例子,
function add(a,b)
{
return a+b;
}
add(5,6);
这类似于下面的咖喱函数,
function add(a)
{
return function(b){
return a+b;
}
}
var curryAdd = add(5);
curryAdd(6);
那么这个代码是什么意思呢?
现在再读一遍定义,
curry是一个包含许多参数的函数,它被重写为接受第一个参数并返回一个函数,该函数反过来使用剩余的参数并返回值。
不过,困惑吗? 让我来详细解释一下!
当你调用这个函数时,
var curryAdd = add(5);
它会返回一个这样的函数,
curryAdd=function(y){return 5+y;}
这叫做高阶函数。也就是说,依次调用一个函数返回另一个函数是高阶函数的精确定义。这是图例的最大优势,Java Script。 回到咖喱,
这一行将把第二个参数传递给curryAdd函数。
curryAdd(6);
结果是,
curryAdd=function(6){return 5+6;}
// Which results in 11
希望你能理解这里咖喱的用法。 那么,说到优点,
为什么鞭笞?
它利用了代码可重用性。 代码越少,错误越少。 你可能会问它是怎么少代码的?
我可以证明它与ECMA脚本6新的特征箭头功能。
是的!ECMA 6,为我们提供了美妙的功能,叫做箭头函数,
function add(a)
{
return function(b){
return a+b;
}
}
借助箭头函数,我们可以将上面的函数写成如下形式:
x=>y=>x+y
酷吧?
所以,更少的代码和更少的错误!!
在这些高阶函数的帮助下,可以很容易地开发出无bug的代码。
我向你挑战!
霍普,你知道什么是咖喱。如果你有任何需要澄清的地方,请在这里评论。
谢谢,祝您愉快!
在这里,您可以找到c#中curry实现的简单解释。在评论中,我试图展示咖喱是如何有用的:
public static class FuncExtensions {
public static Func<T1, Func<T2, TResult>> Curry<T1, T2, TResult>(this Func<T1, T2, TResult> func)
{
return x1 => x2 => func(x1, x2);
}
}
//Usage
var add = new Func<int, int, int>((x, y) => x + y).Curry();
var func = add(1);
//Obtaining the next parameter here, calling later the func with next parameter.
//Or you can prepare some base calculations at the previous step and then
//use the result of those calculations when calling the func multiple times
//with different input parameters.
int result = func(1);
“Currying”是一个获取多个参数的函数并将其转换为一系列函数的过程,每个函数接受一个参数并返回一个参数的函数,或者在最终函数的情况下,返回实际结果。