What is this high-order function called?

I’m in doubt what to call a high-order function that transforms a function so it uses an array instead of an argument-list. It’s complicated to explain in words, so there’s an example in JavaScript:

sum = function(a,b){ return a+b; };
foo = function(fn){
    return function(arr){
        return fn.apply(fn,arr);
    };
};
different_sum = foo(sum);
log(sum(2,3)); //5
log(different_sum([2,3])); //5

  • commenting callback functions
  • JavaScript Style : optional callbacks
  • JavaScript: What dangers are in extending Array.prototype?
  • Defensive programming in node.js using asserts
  • Why should the “key” part in a JS hash/dict be a string?
  • Javascript code style enforcer or checker
  • Is it customary JS to write getters for simple properties?
  • Howto get cross-browser literal style string with JavaScript?
  • I want to know if the two JavaScript snippets are the same or not
  • proper way to construct HTML based on AJAX input
  • Is it better to wrap code into an 'IF' statement, or is it better to 'short circuit' the function and return?
  • Best practice for passing server-side variables to javascript?
  • 5 Solutions collect form web for “What is this high-order function called?”

    There’s a similar method for Scala functions that takes an n-ary function and returns a unary function that takes an n-tuple as the only argument. They call the method tupled, and you can see it in the documentation for Function5 (or any of the other function arities greater than 1).

    In the classic Javascript: The Good Parts this is referenced as the apply invocation pattern, one of the four invocation patterns in Javascript.

    There doesn’t seem to be any generally used term for this.

    The Javascript bind method wraps a function call like that, and there is no special term used.

    The Prototype bind method does the same, and there is no term for it there either.

    The jQuery proxy method does the same, and there is no term for it there either.

    The term “wrapping” is used in the explanations, but that is of course a lot wider term.

    Since it seems like you’re asking “What should I call this function for readability sake?” verus “What type of/classification of function is this?”, I’ll throw in my two cents:

    remixInputMethod()
    

    Really, it’s just a matter of rewiring the way a user of your function interacts with your program. I imagine something like this would be useful in a program where you want to interact with it by passing in input differently from the way you want your users to interact with it.

    this.sum = function(a,b){ return a+b; };
    remixInputMethod = function(fn){
        return function(arr){
            return fn.apply(fn,arr);
        };
    };
    var _sum = remixInputMethod(this.sum);
    
    // Throughout your program, do some more advanced, funky stuff
    var sums = (function buildListOfSums(num){
        var arr = [], output = 0;
        while(num--){
            arr.push(num);
        }
    
        while(arr.length > 2){
            output += _sum([arr[arr.length - 1], arr[arr.length - 2]]);
            arr.pop(arr[arr.length - 1]);
            arr.pop(arr[arr.length - 2]);
        }
        return output;
    }(20));
    console.log(sums);
    
    // But for the user, lets say, who downloaded your API on Github and is using it to
    // build their first webpage for their uncle
    // who thinks they are smarter than bill gates
    sum(1,2);
    

    I would call it a variadic function. If you’re not familiar, variadic functions take a list of arguments and allows the programmer to interact with them as an array.

    Since Javascript doesn’t have support for variadic functions, fn.apply(context, args) is the way to implement it. I don’t think the actual implementation is important though.

    Variadic functions appear in a few different languages, my favorites include:

    • C- var_args
    • D
    • Go

    The D website has a pretty good introduction: http://dlang.org/function.html (search for variadic)

    Edit:

    Your example does the exact opposite of a variadic function. Instead of taking n number of arguments and interacting with them as an array, it’s taking an array of arguments and interacting with them as separate arguments.

    I still argue that this is effectively the same thing as a variadic function, since it takes a variable number of arguments, but it’s just changing the way you interact with those arguments.

    There’s possibly a better term, but maybe we could call it an inverse variadic function?