Skip to content

Instantly share code, notes, and snippets.

@koorchik
Last active December 7, 2017 19:22
Show Gist options
  • Save koorchik/6b904448ee335792457c7fafa6778f85 to your computer and use it in GitHub Desktop.
Save koorchik/6b904448ee335792457c7fafa6778f85 to your computer and use it in GitHub Desktop.
Shorter syntax for arrow functions

Inspired by the Perl6 pointy block short syntax (https://docs.perl6.org/type/Whatever) I like functional programming in JS. And it will be great to have even shorter syntax for lambdas (than arrow functions).

The compiler should detect special syntax and convert it to arrow functions.

Motivation: With shorter syntax it is clearer what is the intent of the code. Moreover, we do not write variable names twice. It is like when you contruct on object obj = {name: name, email: email} you use shorter syntax obj = {name, email}. Here it similar appoach.

Here are some examples. For every example bothe notation are the same.

const multiplyByTwo = & * 2;
const multiplyByTwo = x => x * 2; // will be converted to this

[1, 2, 12, 14].filter(& > 10).map(& * 2); 
[1, 2, 12, 14].filter(x => x > 10).map(x => x * 2 ); // will be converted to this

const adultsEmails = users.filter(&.age >= 18).map(&.email);
const adultsEmails = users.filter(x => x.age >= 18).map(x => x.email); // will be converted to this

What if want to handle several arguments? In this case we can user placeholders (like in Perl6).

array.reduce(&a + &b);
array.reduce((a, b) => a + b); // will be converted to this

array.sort(&a - &b);
array.sort((a, b) => a - b); // will be converted to this

// if we want reverse sort then we can write
array.sort(&b - &a);
array.sort((a, b) => b - a); // placeholder based parameters are always in alphabetical order.

You can use placeholders with single parameter too. Just to increase readability.

const multiplyByTwo = &num * 2;
const multiplyByTwo = num => num * 2; // will be converted to this

[1, 2, 12, 14].filter(&num > 10).map(&num * 2);
[1, 2, 12, 14].filter(num => num > 10).map( num => num * 2 ); // will be converted to this

const adultsEmails = users.filter(&user.age >= 18).map(&user.email);
const adultsEmails = users.filter(user => user.age >= 18).map(user => user.email); // will be converted to this

It is just an idea. I will be glad to get any comments.

How it works:

if compiler finds a variable name with & then it wraps whole expression with arrow function. &a + &b + &c + 10 will converted to (a, b, c) => {return a + b + c + 10}. Parameters are sorted alphabetically, always. So, this code &c + &b + &a + 10 will be converted to (a, b, c) => {return c + b + a + 10}

FAQ:

Q: We have bitwise AND operator "&". How to deal with this? A: It should not be the problem because bitwise "&" is a binary operator, but in our case we have "&" in terms (operands). So this code should work array.map(& & &); should be converted to array.map( x => x & x );. But in this case it is better to write in full notation to make the code clearer.

Edge cases (to check)

ids.map({&id});
ids.map(id => ({ id })); // should convert to this
ids.map(_id1 => ({ id: _id1 })); // if "id" is already defined in outer scope, it should like this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment