Skip to content

Instantly share code, notes, and snippets.

@fanghm

fanghm/js_curry.md

Created Oct 23, 2017
Embed
What would you like to do?
柯里化(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