0

I want to create a function that sorts a function and calls it. The problem is that each function may have a different number of arguments, and these arguments must be passed before the sorting. For example:

function sortFunciton(arrayOfFunctions) {

}

mySortedFunction = sortFunction([function1('a', 'b'), function2('a'), function3('a','b','c')])

The problem is that if I pass the array like this, I'll be passing the value of the function already, but I don't want these functions to be executed before the sorting, because they have heavy computation, so just one must be executed, which is why I'm sorting.

Isn't there a way to pass like this:

mySortedFunction = sortFunction([function1.args('a', 'b', 'c')], function2.args('a'), function3.args('a', 'b', 'c')])

and then do

mysortedFunction.callWithArgs()

?

2
  • 1
    function.prototype.bind may help Commented Jan 13, 2017 at 1:24
  • You cannot compare functions, so I don't know how you would sort them? What exactly do you mean by "so just one must be executed, which is why I'm sorting"? Commented Jan 13, 2017 at 1:31

3 Answers 3

1

You can pass the uninvoked function reference with certain bound arguments by using Function.prototype.bind:

function selectFunc(funcArray) {
  //some selection criteria
  return funcArray[0];
}

mySelectedFunc = selectFunc([function1.bind(null, 'a', 'b'), function2.bind(null, 'a'), function3.bind(null, 'a','b','c')]);

mySelectedFunc() //equivalent of running function1('a', 'b')
Sign up to request clarification or add additional context in comments.

5 Comments

then I just call mySortedFunction()?
following this pattern allows you to haven an array of functions, not an array of the results of the functions. what should sortFunction do?
It must select one of these functions to be invoked with these arguments.
ah gotcha - so yea you could just call mySortedFunction() - will update answer
@Gatonito, hackerrdave, binding this is fast as ****. Using bind() to partially apply a function (what you show) is way slower than a closure (in modern JS-engines). I'd recommend implementing the functions as higher order functions in the first place: instead of function fn1(arg1, arg2, a, b){ return a-b } and binding arg1 and arg2, function fn1(arg1, arg2){ return function(a, b){ return a-b} } and calling it as var sortFn = fn1("foo", "bar");
0

You can use .bind then just call your function without arguments (it will be called with the parameters that have been bound)

function add(a, b) { return a + b; }
function mult(a, b) { return a * b; }
var functions = [add.bind(this, 1, 1), mult.bind(this, 5, 5)];

functions.forEach(f => console.log(f()));

Comments

0
function sortFunciton(arrayOfFunctions) {

}

var mySortedFunction = sortFunction([
    { func: function1, arguments: ['a', 'b']}, 
    { func: function2, arguments: ['a']}, 
    { func: function3, arguments: ['a', 'b', 'c']}
]);

for (var i = 0; i < mySortedFunction.length; i++)
{
    mySortedFunction[i].func.apply(this, mySortedFunction[i].arguments);
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.