Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pre-set arguments in JavaScript function call? (Partial Function Application)

I am trying to write a JavaScript function that will return its first argument(function) with all the rest of its arguments as preset parameters to that function.

So:

function out(a, b) {     document.write(a + " " + b); }  function setter(...) {...}  setter(out, "hello")("world"); setter(out, "hello", "world")(); 

Would output "hello world" twice. for some implementation of setter

I ran into an issue with manipulating the arguments array on my first try, but it seems there would be a better way to do this.

like image 700
AlexH Avatar asked Nov 26 '08 15:11

AlexH


People also ask

How can we pass arguments in function calling?

The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. By default, C programming uses call by value to pass arguments.

Can we pass a function as argument of another function in JavaScript?

Passing a function as an argument to the function is quite similar to the passing variable as an argument to the function. so variables can be returned from a function.

What is partial application in JavaScript?

Partial application allows us to fix a function's arguments. This lets us derive new functions, with specific behavior, from other, more general functions. Currying transforms a function that accepts multiple arguments “all at once” into a series of function calls, each of which involves only one argument at a time.

How do you make a function take any number of arguments JavaScript?

When you call a function in JavaScript, you can pass in any number of arguments, regardless of what the function declaration specifies. There is no function parameter limit. In the above function, if we pass any number of arguments, the result is always the same because it will take the first two parameters only.


1 Answers

First of all, you need a partial - there is a difference between a partial and a curry - and here is all you need, without a framework:

function partial(func /*, 0..n args */) {   var args = Array.prototype.slice.call(arguments, 1);   return function() {     var allArguments = args.concat(Array.prototype.slice.call(arguments));     return func.apply(this, allArguments);   }; } 

Now, using your example, you can do exactly what you are after:

partial(out, "hello")("world"); partial(out, "hello", "world")();  // and here is my own extended example var sayHelloTo = partial(out, "Hello"); sayHelloTo("World"); sayHelloTo("Alex"); 

The partial() function could be used to implement, but is not currying. Here is a quote from a blog post on the difference:

Where partial application takes a function and from it builds a function which takes fewer arguments, currying builds functions which take multiple arguments by composition of functions which each take a single argument.

Hope that helps.

like image 50
Jason Bunting Avatar answered Sep 20 '22 21:09

Jason Bunting