Future JavaScript : partial-application (using ~(?,?))

Future JavaScript : partial-application (using ~(?,?))

partial-application is a technique of producing another function from defined function by fixing a number of arguments in the defined function.

you can do this by the built in method .bind or with currying technique :

.bind method

function add(x, y) { return x + y; }

// Function.prototype.bind
const addOne = add.bind(null, 1);
addOne(2); // 3

// arrow functions
const addTen = x => add(x, 10);
addTen(2); // 12

currying technique

const add =  x => y =>x+y

// currying 
const addOne = add( 1);
addOne(2); // 3

// seconde example
const addTen = x => add(10);
addTen(2); // 12

However, there are several of limitations with these approaches:

-1- .bind methode require you explicitly specify the this receiver
-2- currying technique may cause callback hell

To resolve these concerns, javascript introduce new language features :

  • A new calling convention,~() , to indicate a partial application of a call or new expression which results in a partially applied function.

  • Using the ? token to act as a placeholder argument for any non-fixed argument in a partial application.

const add = (x, y) => x + y;

// apply from the left:
const addOne = add~(1, ?);
addOne(2); // 3

// apply from the right:
const addTen = add~(?, 10);
addTen(2); // 12

This proposal is currently on stage 1 , if you wanna more details you can find them here : l[github.com/tc39/proposal-partial-applicatio..

enjoy !!