js实现a(3)(4)(4)=48

charles 3月29日提问
0

js实现a(3)(4)(4) // print 48

15个回答

9

``````        // 固定参数实现
const result = x => y => z => x * y * z;
result(3)(4)(4) // 48;
// 柯里化实现
function curry(fn) {
const len = fn.length;
return function curried() {
const args = Array.prototype.slice.call(arguments);
if (args.length >= len) {
return fn.apply(this, args);
}
return function () {
return curried.apply(this, args.concat(Array.prototype.slice.call(arguments)));
};
};
}
const result = curry(function (a, b, c) {
return a * b * c;
});
result(3)(4)(4); // 48
``````
8
``````var f = (function() {
let r = 1;
return function f(...args){
r *= args.reduce((p, n) => p * n);
f.valueOf = () => r;
return f;
}
}());
f(3)(4)(4)``````
7
最简单最完美实现
``````// es6
const mul = (x) => {
let sum = x
let curried = (y) => {
sum = sum * y
return curried
}
curried.toString = () => sum
return curried
}
console.log(mul(1)) //1
console.log(mul(1)(2))//2
console.log(mul(1)(2)(3))//6
console.log(mul(1)(2)(3)(4))//24
...

// es5
function mul(x){
var sum = x
var curried = function(y){
sum = sum * y
return curried
}
curried.toString=function(){
return sum
}
return curried
}
console.log(mul(1)) //1
console.log(mul(1)(2))//2
console.log(mul(1)(2)(3))//6
console.log(mul(1)(2)(3)(4))//24
...
``````
3
``const a = () => () => () => 48``
2
``````var a = function(x) {
return function(y) {
return function(z) {
console.log(x * y * z);
return x * y * z;
};
};
};``````
2
• 识别考点：明显是函数柯里化
• 概念：函数柯里化的基本形式，就是将一个多参数的函数，转为单个参数的函数多次调用。

``multiply(3,4,4) 转为 multiply(3)(4)(4)``
• 解题：所以最直接的方式就是
``````将一个普通的多参数定义，如
function multiply(a,b,c){
return a*b*c
}

function multiply(a){
return function(b){
return function(c){
return a*b*c
}
}
}

const multiply = a=>b=>c=>a*b*c``````
2
Function Currying

Simple, Beautiful, Interesting

``````function curry(fn, ...args){
return args.length === fn.length ?
fn(...args) :
(...next_args) => curry(fn, ...args, ...next_args);
}``````

Next

``````function mul(a, b, c){
return a * b * c;
}
// mul(3, 4, 4); => 48

let a = curry(mul);

a(3)(4)(4);
// => 48

a(3, 4, 4);
// => 48

a(3)(4, 4);
// => 48

a()()()()()(3, 4)(4);
// => 48 ``````

S

// 来晚了 ...

1
``````let total;
function a(m){
total = m;
return function(n){
return a(m*n)
}
}

a(3)(4)(4)
console.log(total)  //48
``````
1

1

gang · 4月4日

0

0
``const a = x => y => z => x * y * z``
0
``````const a = x => Object.assign(
y => a(x * y),
{
valueOf: () => x,
toString: () => String(x),
}
)

a(3)(4)(4) == 48         // ==> true
String(a(3)(4)(4))       // ==> "48"
console.log(a(3)(4)(4))  // ==> f 48``````
0

``````const curry3 = fn=> a => b => c => fn(a,b,c);
const mutl3 = (a, b, c) => a * b * c;
const curried3 = curry3(mutl3);
let result = curried3(3)(4)(4);
console.log(result);``````

``````const curry = fn => {
let _args = [];
const _fn = (...args) => {
_args.push(...args);
return args.length >= 1 ? _fn : fn(..._args);
}
return _fn;
}
// 累乘
const multiplication = curry(function() {
return Array.prototype.reduce.call(arguments, (acc, val) => {
return acc * val;
}, 1);
});
let result = multiplication(3)(4)(4)();
console.log(result);``````
-2