1

As a functional programmer I regularly pass functions to higher order functions, to combine them in a specific way and create new behavior. Often HOFs serve as wrappers that – as a side effect – disguise the original properties of the given function. A curry function is a good example for that:

// any arbitrary curry function:
function curry(f) {
  function curried() {
    if (arguments.length < f.length) {
      var args = Array.prototype.slice.call(arguments);
      return function () {
        return curried.apply(null, args.concat(Array.prototype.slice.call(arguments)));
      }
    } else {
      return f.apply(null, arguments);
    }
  }

  return curried;
}

function sum(x, y, z) {
  return x + y + z;
}

// reflection in effect:
sum.name; // sum
sum.length; // 3

sum = curry(sum);

// application:
sum(1, 2, 3); // 6
sum(1)(2)(3); // 6
sum(1, 2)(3); // 6

// disguised reflection:
sum.name; // curried
sum.length; // 0

I wonder if this reflective ability should be preserved throughout the code, even though this may result in higher complexity. Or is it poor style to build dependencies against such properties anyway?

If you think that this question is mainly opinion based, just answer the following:

Are there use cases for length and name, which demonstrate the necessity to preserve them in the entire code base?

  • 1
    IMO, the only value in JS's current reflection abilities are in the `arguments` object, especially in terms of currying. Reflection is just around the corner (https://ponyfoo.com/articles/es6-reflection-in-depth), but right now I feel like it's lacking and too inconsistent to rely on throughout any non-trivial application. – Rob M. Feb 23 '16 at 18:27
  • Possible duplicate of [How should I name my javascript library functions so they are differentiable from plain old javascript?](http://stackoverflow.com/questions/10694151/how-should-i-name-my-javascript-library-functions-so-they-are-differentiable-fro) – Paul Sweatte Mar 15 '16 at 01:29
  • @Paul My question is about reflection associated with functions and not naming conflicts. –  Mar 15 '16 at 10:37

0 Answers0