Giter Club home page Giter Club logo

proposal-partial-expression's Introduction

Partial Expression Syntax for ECMAScript

This proposal introduces a new syntax using the #, ? (any number of ?s after each other), ?x (x ∈ ℕ), ?r tokens which allow you to partially apply an expression by acting as placeholders for a value or values.

Status

Stage: #1
Champion: #1

Any and all feedback and ideas are greatly appreciated. Use the issues to post questions/ideas and send pull requests for any content updates. Fixes, clarifications, and especially usage examples are definitely helpful.

For more information see the TC39 proposal process.

Authors

Some examples which regularly come up

[1,2,3,4,5,6,7,8,9].map(#1+?); // partially apply an operator
//=> [2,3,4,5,6,7,8,9,10]

[1,2,3,4,5,6,7,8,9].reduce(#?+?); // use an operator as a function
//=> 45

['cat', 'coconut', 'carrot', 'dog', 'sun'].filter(#?.startsWith('c')); // partially apply a function/method
//=> ['cat', 'coconut', 'carrot'] 

Proposal

# and ?

The # operator (precedence: 3.5) makes the affected expression a function. All upcoming tokens are only interpreted in this expression.

The ? is a variable which will have the value of the corresponding argument. The first one stands for the first argument, the second for the second, etc. The order is determined by the order of their appearance in the code.

const add = #?+?;
// const add = (x,y) => x+y;

const addOne = #1+?;
// const addOne = (x) => 1+x;

?x (?0, ?1, ?2)

The number specifies the ordinal position of the parameter. They allow the usage of an argument more than once & swap their order. Numbering starts from 0. The unnumbered ?s fill out the left out parameters from left to right.

const power = #?0**?1;
// const power = (x,y) => x**y;

const basicTetration = #?0**?0;
// const basicTetration = x => x**x;

const flippedPower = #?1**?0;
// const flippedPower = (x,y) => y**x;

const foo = #bar(?1,?2,?,?1,?,?4,...?r,?);
// const foo = (x0,x1,x2,x3,x4,x5,...xs) => bar(x1,x2,x0,x1,x3,x4,...xs,x5);

?r

The ?r is an array of the arguments after the last used parameter.

const maxWithTheMinOf0 = #Math.max(0, ?, ...?r, ?);
// const maxWithTheMinOf0 = (x,y,...zs) => Math.max(x,...zs,y)

const foo = #Math.max(0, ?, ...?r, ?2);
// const maxWithTheMinOf0 = (x,_,y,...zs) => Math.max(x,...zs,y)

Parsing

Conditional expressions:
The ? token is used in conditional expressions, but its not ambigous, because in this proposal, a ? cannot follow an expression, while in a conditional expression it always does. (e.g. in f(a? the ? is definitely a conditional operator while in f(? it's definitely a placeholder).

Optional chaining:
The operator ?. must appear directly after an expression, while the ? variable can't.

TODO

The following is a high-level list of tasks to progress through each stage of the TC39 proposal process:

Stage 1 Entrance Criteria

  • Identified a "champion" who will advance the addition.
  • Prose outlining the problem or need and the general shape of a solution.
  • Illustrative examples of usage.
  • High-level API (proposal does not introduce an API).

Stage 2 Entrance Criteria

Stage 3 Entrance Criteria

Stage 4 Entrance Criteria

  • Test262 acceptance tests have been written for mainline usage scenarios and merged.
  • Two compatible implementations which pass the acceptance tests: [1], [2].
  • A pull request has been sent to tc39/ecma262 with the integrated spec text.
  • The ECMAScript editor has signed off on the pull request.

Related proposals

It would play nicely with the pipeline operator.
Or you might be interested in other partial application proposals:

proposal-partial-expression's People

Contributors

trustedtomato avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

mikesamuel

proposal-partial-expression's Issues

Rename this proposal?

I think a name e.g. 'Arrow Function Shorthand Syntax' would be better because this proposal essentially is to introduce a shorthand syntax for arrow functions.

Ambiguity of the ternary operator with ??

The code #?? +1 : 2 needs a lookahead for the parsing, and that lookahead might be at any size, since the code could be #?? +11111111111111 : 2 too.
Any idea for another notation?
Or is there a need for them at all?

Mike Samuel mentioned the bug.

Precedence of the # operator

Originally I thought the precedence should be between the Logical OR (… || …) and the Conditional (… ? … : …) as it would seem very wrong if #? || true would translate to (x => x) || true or #? ? ? : ? would translate to (x,y,z) => x ? y : z... (ternary operator discussion: #2)
But now I think that actually makes sense, so it should have the precedence of 3.5 (right above assignment). What are your thoughts?

Comparison with smart-pipelines proposal

Hi, I’m the author of a smart pipelines proposal. It has already been formally specified, it has a Babylon plugin under development, and it will be presented to the TC39 by @littledan next week alongside the original pipelines proposal and an alternative pipeline proposal.

The smart pipelines proposal has a “topic style” that is very similar to your proposal. (N-ary expressions are deferred to an “additional feature”.)

Right now I’m hurrying to help prepare the presentation and the Babel plugin for TC39, so I don’t have free time to examine your proposal in detail right now. But I will definitely take a close look at it sometime. In the meantime, I would love to hear your response to the similarity between our proposals. 👍

Replacing the # symbol

Private fields in the Class field declarations proposal does seem to conflict with the # symbol, and it's in Stage 3. So the # should be replaced with a symbol which is one of these: !@#$%^&*()_+-=[]{};:'",<.>/?

Pointed out by Isiah Meadows & Sebastian Cholewa.

Adding usage examples

We should add more examples to the example directory. The examples should be readable. If the example is showing how a code looks like using this syntax vs not using the syntax, the one using the syntax should be more readable.

Babel plugin

Hey, great work on the proposal!

I created a babel plugin with simpler rules ("simpler", not "better"!), exploring no prefix partial expressions:

let bang = _ + '!';
// equals to
let bang = x => x + '!';

You can play with it in the babel playground.

And here's the plugin repository where I described some rules and how it integrates with the pipeline operator.

Maybe it can help pushing the proposal further.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.