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] 第57天 写一个方法,使得sum(x)(y)和sum(x,y)返回的结果相同 #238
Comments
在 JavaScript 高阶函数浅析 获取的灵感 |
function sum(...args) {
function curriedSum(...rest) {
args.push(...rest);
return curriedSum;
}
curriedSum.toString = () => args.reduce((prev, curr) => curr + prev);
curriedSum.toNumber = () => args.reduce((prev, curr) => curr + prev);
return curriedSum;
} |
类似于python元编程,修改toString 和toNumber 方法,使其既可以当做数字也可以当做函数调用 |
虽然我一时半会没想出什么解决方案但是你这个无限 curry 的不太行啊,除了显然的 typeof 结果不一样以外,还有这种,所以得到的值并不能放心用: sum(1,2,3).toExponential(3);
// "6.000e+0"
sum(1)(2)(3).toExponential(3);
// VM1727:1 Uncaught TypeError: sum(...)(...)(...).toExponential is not a function
// at <anonymous>:1:14 |
作者 怎么也不最后给个答案呀 |
|
@Gxmg 没有最后的答案 |
此题考函数式编程: console.log(sum(1,3)); console.log(sum(1)(3)); |
Lodash _.curry() |
@yuqingc 那你自己来实现一个呢? |
//函数柯理化
function sum1(x, y, z) {
return x + y + z
}
function sum2() {
var args = arguments
return function () {
var arr = []
arr.push(...args, ...arguments)
return arr.reduce((total, item) => total += item, 0)
}
}
console.log(sum1(6, 9, 14)) //29
console.log(sum2(6)(9, 14)) //29 |
function currying(fn) {
let args = [];
return function closureFn() {
args = args.concat(Array.from(arguments));
if (args.length < fn.length) return closureFn;
const res = fn.apply(this, args);
args = [];
return res;
}
}
const sum = currying((a, b) => a + b);
console.log(sum(1, 2) === sum(1)(2)); // true |
综合一下大家的写法,再自由发挥一下~话说怎么让代码高亮啊???
|
function add(){ |
function sum(a,b){ |
function curry(...args) {
if (args.length > 1) return args2.reduce((acc, cur) => acc + cur, 0)
let val = 0
function add(...args2) {
val = args2.reduce((acc, cur) => acc + cur, val)
return add
}
add.toString = function() { return val }
val = args[0]
return add
}
// curry(1,6)(2,5)(3,4)(7,8,9) -> 45
// curry(1) -> 1 |
function Curry(fn, length) {
var length = length || fn.length;
return function (...arg) {
if (arg.length < length) {
return Curry(fn.bind(this, ...arg), length - arg.length);
} else {
return fn(...arg)
}
}
}
函数式编程 柯里化 |
function sum(x,y) {
return x + y;
}
sum(2,3)
const sum = x => y => x + y
function sum (x) {
return function(y) {
return x + y
}
}
sum(2)(3) |
|
|
export const sum = (...x) => {
let result = x.reduce((a, b) => a + b);
const add = (...y) => {
if (y.length === 0) {
return result;
}
result += y.reduce((a, b) => a + b);
return add;
};
return add;
};
console.log(sum(1, 2, 3, 4)()); // 10
console.log(sum(1)(2)(3)(4)()); // 10
console.log(sum(1, 2)(3)(4)()); // 10
console.log(sum(1)(2, 3, 4)()); // 10 |
柯里化函数 |
const sum = (a , b) => {
if (b) {
return a + b;
} else {
return (another) => {
return a + another;
}
}
} |
第57天 写一个方法,使得sum(x)(y)和sum(x,y)返回的结果相同
The text was updated successfully, but these errors were encountered: