为什么这样构造得到的函数长度为0?

作者站长头像
站长
· 阅读数 22
function pipe(...fns) {
  if (arguments.length === 0) {
    throw new Error('pipe requires at least one argument');
  }
  return function(...param) {
    return fns.reduce((pre, fn) => {
      if (Array.isArray(pre)) {
        return fn.apply(this,pre);
      }
      return  fn.apply(this,[pre]);
    }, param);
  };
}
var f = function(a, b, c) { return [a, b, c]; };
var g = pipe(f);
console.log('g: ', g.length);
var f1 = pipe(parseInt, R.multiply, R.map);
console.log('g: ', f1.length);
var assert = require('assert');

var R = require('../source/index.js');
var eq = require('./shared/eq.js');


describe('pipe', function() {

  it('is a variadic function', function() {
    eq(typeof R.pipe, 'function');
    eq(R.pipe.length, 0);
  });

  it('performs left-to-right function composition', function() {
    //  f :: (String, Number?) -> ([Number] -> [Number])
    var f = R.pipe(parseInt, R.multiply, R.map);

    // eq(f.length, 2);
    eq(f('10')([1, 2, 3]), [10, 20, 30]);
    eq(f('10', 2)([1, 2, 3]), [2, 4, 6]);
  });

  it('passes context to functions', function() {
    function x(val) {
      return this.x * val;
    }
    function y(val) {
      return this.y * val;
    }
    function z(val) {
      return this.z * val;
    }
    var context = {
      a: R.pipe(x, y, z),
      x: 4,
      y: 2,
      z: 1
    };
    eq(context.a(5), 40);
  });

  it('throws if given no arguments', function() {
    assert.throws(
      function() { R.pipe(); },
      function(err) {
        return err.constructor === Error &&
               err.message === 'pipe requires at least one argument';
      }
    );
  });

  it('can be applied to one argument', function() {
    var f = function(a, b, c) { return [a, b, c]; };
    var g = R.pipe(f);
    // eq(g.length, 3);
    eq(g(1, 2, 3), [1, 2, 3]);
  });

});

上述代码是我在进行 ramda 函数库的 pipe 实现过程中的代码。我无法通过测试用例 performs left-to-right function composition和 can be applied to one argument

我发现是因为我是实现的代码 f的长度都是0,所以我想问什么原因造成的这个问题,如何解决?

ramda - test -pipe

回复
1个回答
avatar
test
2024-06-28

因为length属性表示该函数预期传入的参数个数,而不是函数定义中声明的参数个数。而在你的代码中,函数 f 接收三个参数,但是它并没有显式地声明这些参数,所以它的 length 属性为 0,这是符合预期的。简单说就是属性没有定义。

var f = R.pipe(R.nAry(3, parseInt), R.multiply, R.map);

在f这里定义一个3就可以解决这个问题了。

回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容