Support argument length binding

Within Javascript, an area that is not very ergonomic when practicing functional-style programming. One key problem in this area is that passing around pre-bound functions in javascript does not allow you to restrict the arguments passed into the function.

A very clear-cut case is when you have an array of strings and you want to parse each string into an integer. You'd like to use parseInt for example but because Array.prototype.map passes additional arguments you cannot pass it in directly.

Here I'd like to propose the idea that there is 1) an API to restrict the argument length similar to how you can bind arguments.

const strings = ['1', '2', '3', '4', '5']
const numbers = strings.map(parseInt.bindLength(1)) 

First, consider the bindLength method, which creates a wrapper around the function it is invoked on and the wrapper function cuts off the arguments past the given length.

Function.prototype.bindLength = function(maxLength) {
  const fn = this;
  return function(...args) {
    return fn.call(this, ...args.slice(0, maxLength));
  }
}

Then, if you will endulge me, I also considered making this an operator (for example, /).
This makes functions look like the arity notation from Elixir / Erlang.

const numbers = strings.map(parseInt/1)

Related:

This is already valid syntax so its semantics can't change.

This is longer and less clear than strings.map(x => parseInt(x)).

1 Like

Fair enough, the real crux I'd like to put forth is a compacter way to do partial application. Thanks @aclaymore for the proposal link. I guess my next best action would be to get involved there. It looks like it has been dormant for some years, there are some option issues with stranded discussions and the next step according to the proposal itself is to get formalized. Does anyone have suggestions on what I could do for the proposal at this time?

If interested in a proposals that have been around for many years, the main thing I would recommend is reading through the transcripts from the previous meetings. I think the most recent discussion on partial application is here: