我在一些文章和博客中看到了curry函数的引用,但我找不到一个好的解释(或者至少一个有意义的解释!)


当前回答

它可以是一种用函数生成其他函数的方法。

在javascript中:

let add = function(x){
  return function(y){ 
   return x + y
  };
};

我们可以这样称呼它:

let addTen = add(10);

运行时,将10作为x传入;

let add = function(10){
  return function(y){
    return 10 + y 
  };
};

这意味着我们返回这个函数:

function(y) { return 10 + y };

所以当你打电话的时候

 addTen();

你真的在呼唤:

 function(y) { return 10 + y };

如果你这样做:

 addTen(4)

这就相当于:

function(4) { return 10 + 4} // 14

所以我们的addTen()总是给我们传入的任何东西加10。我们可以用同样的方法来构造类似的函数:

let addTwo = add(2)       // addTwo(); will add two to whatever you pass in
let addSeventy = add(70)  // ... and so on...

接下来的问题是,你到底为什么要这么做?它将一个急迫的操作x + y变成了一个可以轻松完成的操作,这意味着我们至少可以做两件事 1. 缓存昂贵的操作 2. 在功能范式中实现抽象。

想象我们的咖喱函数是这样的:

let doTheHardStuff = function(x) {
  let z = doSomethingComputationallyExpensive(x)
  return function (y){
    z + y
  }
}

我们可以调用这个函数一次,然后将结果传递给很多地方,这意味着我们只做一次计算上昂贵的事情:

let finishTheJob = doTheHardStuff(10)
finishTheJob(20)
finishTheJob(30)

我们可以用类似的方式得到抽象。

其他回答

在这里,您可以找到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);

它可以是一种用函数生成其他函数的方法。

在javascript中:

let add = function(x){
  return function(y){ 
   return x + y
  };
};

我们可以这样称呼它:

let addTen = add(10);

运行时,将10作为x传入;

let add = function(10){
  return function(y){
    return 10 + y 
  };
};

这意味着我们返回这个函数:

function(y) { return 10 + y };

所以当你打电话的时候

 addTen();

你真的在呼唤:

 function(y) { return 10 + y };

如果你这样做:

 addTen(4)

这就相当于:

function(4) { return 10 + 4} // 14

所以我们的addTen()总是给我们传入的任何东西加10。我们可以用同样的方法来构造类似的函数:

let addTwo = add(2)       // addTwo(); will add two to whatever you pass in
let addSeventy = add(70)  // ... and so on...

接下来的问题是,你到底为什么要这么做?它将一个急迫的操作x + y变成了一个可以轻松完成的操作,这意味着我们至少可以做两件事 1. 缓存昂贵的操作 2. 在功能范式中实现抽象。

想象我们的咖喱函数是这样的:

let doTheHardStuff = function(x) {
  let z = doSomethingComputationallyExpensive(x)
  return function (y){
    z + y
  }
}

我们可以调用这个函数一次,然后将结果传递给很多地方,这意味着我们只做一次计算上昂贵的事情:

let finishTheJob = doTheHardStuff(10)
finishTheJob(20)
finishTheJob(30)

我们可以用类似的方式得到抽象。

我发现这篇文章,以及它引用的文章,有助于更好地理解咖喱: http://blogs.msdn.com/wesdyer/archive/2007/01/29/currying-and-partial-function-application.aspx

正如其他人所提到的,它只是一种具有单参数函数的方法。

这很有用,因为你不需要假设有多少参数将被传入,所以你不需要2个参数,3个参数和4个参数函数。

下面是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));
  };