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

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

阅读 28k
15 个回答

函数柯里化 了解一下?

        // 固定参数实现
        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
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)
最简单最完美实现
// 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
    ...
const a = () => () => () => 48
var a = function(x) {
    return function(y) {
      return function(z) {
          console.log(x * y * z);
          return x * y * z;
      };
    };
};
  • 识别考点:明显是函数柯里化
  • 概念:函数柯里化的基本形式,就是将一个多参数的函数,转为单个参数的函数多次调用。

例如

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
        }
    }
}

使用ES6的写法就是
const multiply = a=>b=>c=>a*b*c
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

clipboard.png

// 来晚了 ...

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

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

链式执行返回值必定为函数

想深入了解问题本质,建议了解下 柯里化

const a = x => y => z => x * y * z
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

固定3个参数的的柯里化。。。

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);
新手上路,请多包涵

没法实现 。 如果 a(3)(4)===12 那么 typeof(a(3)(4))必定是number 那么number必定不能像函数一样调用

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏