Skip to content

Instantly share code, notes, and snippets.

@fanghm
Created October 23, 2017 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fanghm/7de17b3560bb2298b63452fb630b453e to your computer and use it in GitHub Desktop.
Save fanghm/7de17b3560bb2298b63452fb630b453e to your computer and use it in GitHub Desktop.
柯里化(curry)

curry 的概念很简单:只传递给函数一部分参数来调用它,让它返回一个函数去处理剩下的参数。

function isBiggerThan(value) {
  return function(data) {
    return data >= value;
  }
}
[12, 5, 8, 130, 44].filter(isBiggerThan(10)).filter(isBiggerThan(50));

Use lodash:

var curry = require('lodash').curry;
var isBiggerThan = curry(function(value, data) {
    return data >= value;
});
[12, 5, 8, 130, 44].filter(isBiggerThan(10)).filter(isBiggerThan(50));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment