-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[js] 实现一个自动化柯里化方法 #24
Comments
|
贴个coco的解法思路,挺抖机灵的: function add () {
console.log('进入add');
var args = Array.prototype.slice.call(arguments);
var fn = function () {
var arg_fn = Array.prototype.slice.call(arguments);
console.log('调用fn');
return add.apply(null, args.concat(arg_fn));
}
fn.valueOf = function () {
console.log('调用valueOf');
return args.reduce(function(a, b) {
return a + b;
})
}
return fn;
} |
之前写过的一个: /**
* 柯里化的一种实现方式
*/
function currying (...arg) {
val = arg.reduce((cur, old) => cur + old, 0)
return Object.assign(currying.bind(this, val), {
valueOf () {
return val
},
toString () {
return val
},
toLocaleString () {
return val
}
})
}
var a = currying(1)(2)
// 因为console.log实现的问题。。所以必须在前边添加一个+来确保能够得到正确的输出
console.log(+a, +a(4), +a(5, 6)) 或者一个较为通用的实现方法:https://github.com/Jiasm/notebook/blob/master/currying.js |
function curry(fn) {
const _inner = (...args1) => args1.length < fn.length
? (...args2) => _inner(...args1, ...args2)
: fn(...args1)
return _inner
}
const curriedSum = curry((x, y, z) => x + y + z); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
思考下如何实现这个 curry 函数,不建议参考别人代码
The text was updated successfully, but these errors were encountered: