1

I'm trying to find out if there's a concept or an existing formal name for this use of partial function application/binding.

I've seen it in multiple places/libraries in different names but here's more or less the concepts I'm talking about vs. the normal curry/bind/partial function application:

//Typical usage of bind/curry:
fn = bind(function(){console.log(this, arguments)}, context, 'arg 1', 'arg 2');

fn('arg 3'); //logs out arg 1, arg 2, and arg 3

//What I'm looking for:
fn = makeFn(function(){console.log(this, arguments)}, context, 'arg 1', 'arg 2');

fn('arg 3'); //logs out arg 1, arg 2
// (Notice that arg 3 is ignored because it's not in the defined list of arguments).

An example use case for something like this:

obj = {
toggle: function(force, config){
node[force || this.hidden ? 'show' : 'hide'](config && config.someSetting);
this.hidden = !this.hidden;
},
hidden: false
}

node.on('click', makeFn(obj.toggle, obj, true));

(using bind/curry pass along an event object or whatever other arguments are sent in, but the function we're defining would be trying to optionally use that as a completely different type of argument).

I've seen this pattern called multiple things in different libraries with some slightly different syntaxes/abilities.

Facebook's js uses Function.prototype.shield(context, [args...]), Mootools uses Function.prototype.pass(args, [context]), Sencha (née ExtJS) has Function.prototype.createDelegate(fn, args, [appendArgs = false]) and I've even seen some called partial(fn, context, args) (however partial usually has the added ability to pass around a placeholder to allow the passing of different default arguments; I'm avoiding this due to the performance overhead of looping over the argument list on every function call).

Is this a formal concept, or is it only an implied one that requires some new name?

Thanks in advance :)

Nate Cavanaugh
  • 1,137
  • 1
  • 9
  • 15
  • Imo it is still a partial function application. But instead of fixing *some* arguments, you fix *all*, resulting in a function which does not accept any further arguments. From Wikipedia: *"(...)partial function application refers to the process of fixing a number of arguments to a function, producing another function of smaller arity. "*. In your example, your function basically accepts an arbitrary number of arguments, but if you have a "properly" defined function `function(a, b)` which only operates on the first two arguments, then the third argument is ignored. – Felix Kling Aug 22 '11 at 00:09
  • Thanks Felix, but the part where I'm not sure if it fits is that in the method I described, the function arity could actually be larger. For instance, if there are only 2 defined arguments, but the function still accesses an optional third via `arguments[2]`. Since it's saying that the produced function has a smaller arity, how would you say it applies in cases like this? – Nate Cavanaugh Aug 22 '11 at 04:39
  • Possible duplicate of [Function signature of Tap (K-combinator)](https://stackoverflow.com/questions/38614679/function-signature-of-tap-k-combinator) – Paul Sweatte Sep 12 '17 at 19:07

0 Answers0