【问题标题】:sum(2)(3) and sum(2, 3) what is the common solution for bothsum(2)(3) 和 sum(2, 3) 两者的共同解决方案是什么
【发布时间】:2019-10-01 19:25:29
【问题描述】:

我在一次采访中被问到这个问题。

sum(2)(3) 的柯里化风格

sum(a) {
  return (b) {
    return a + b;
  }
}

求和 (2, 3)

sum(a, b) {
  return a + b;
}

有什么共同的功能可以同时工作

【问题讨论】:

  • 你可以做一个,但它会很丑,而且恕我直言糟糕的代码风格。只需检查第二个参数是否存在,如果没有,则返回函数,否则返回结果。
  • 检查你得到了多少arguments,然后返回结果或内部函数。
  • 最大“统一”:这可以通过代码中的单个“+”来解决。
  • 肯定有重复的地方,我知道我以前回答过这个问题...

标签: javascript currying


【解决方案1】:

这是一个可以从任何非柯里化函数创建广义柯里化函数的函数。它是在不使用任何 ECMAScript 6 语法的情况下编写的。无论原始函数预期的参数数量如何,或者为每个部分应用程序提供的参数数量如何,这都有效。

function sum (a, b) {
  return a + b;
}

function product (a, b, c) {
  return a * b * c;
}

function curry (fn) {
  return function partial () {
    return arguments.length >= fn.length
      ? fn.apply(this, arguments)
      : partial.bind.apply(partial, [this].concat(Array.prototype.slice.call(arguments)));
  };
}

var s = curry(sum);

console.log(s(1, 2));
console.log(s(1)(2));
console.log(s()()(1)()()(2));

var p = curry(product);

console.log(p(2, 3, 4));
console.log(p(2)(3)(4));
console.log(p()()(2)()()(3, 4));

【讨论】:

    【解决方案2】:

    有一种方法,但它很hacky......

    您始终可以检查是否已将第二个参数传递给您的函数并做出相应的反应

    function sum(a, b){
        if(b === undefined){
            return (c) => {
                return c + a;
            }
        }
    
        return a + b;
    }
    

    【讨论】:

      【解决方案3】:

      您可以根据arguments 对象的length 返回总和或函数。

      function sum(a,b) {
        return arguments.length === 2   //were two arguments passed?
          ? a+b                         //yes: return their sum
          : (b) => a+b                  //no:  return a function
      };
      
      console.log(sum(3)(5));
      console.log(sum(3,5));

      【讨论】:

        【解决方案4】:

        你可以有一个函数同时使用无限柯里化:

        这里的想法是每次返回一个函数以及一个计算值,这样如果再次调用,返回的函数将处理它,如果没有调用,则打印计算值。

        function sum(...args) {
          function add(...args2) {
            return sum(...args, ...args2);
          }
        
          const t = [...args].reduce((acc, curr) => acc + curr, 0);
          add.value = t;
        
          return add;
        }
        
        const result1 = sum(2, 3).value;
        const result2 = sum(2)(3).value;
        const result3 = sum(2, 3)(4).value;
        const result4 = sum(2, 3)(4, 5).value;
        const result5 = sum(2, 3)(4, 5)(6).value;
        
        console.log({ result1, result2, result3, result4, result5 });

        【讨论】:

          【解决方案5】:

          您必须检查是否定义了第二个参数:

          const sum = (a,b) => {
            const add1 = (b) => {
                return a+b
            }
          
          return typeof b == 'undefined' ? add1 :   add1(b)   \\if it has the second arg else if it hasn't the second arg so give it the arg\\
          

          }

          【讨论】:

            猜你喜欢
            • 2020-08-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-02-26
            • 2017-08-12
            • 2015-12-07
            • 1970-01-01
            • 2020-08-05
            相关资源
            最近更新 更多