Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace pipeline with `previous value` #156

Open
bfred-it opened this issue Jul 8, 2019 · 8 comments

Comments

Projects
None yet
5 participants
@bfred-it
Copy link

commented Jul 8, 2019

This example is simple enough:

let result = "hello"
  |> doubleSay
  |> capitalize
  |> exclaim;

But more complex examples with inline functions and await are not particularly readable:

let newScore = person.score
  |> double
  |> (_ => add(7, _))
  |> (_ => boundScore(0, 100, _));

What if instead of a pipeline, we have a magic variable to have the result of the previous operation?

double(person.score);
add(7, #);
let newScore = boundScore(0, 100, #);

Shorter, no special syntax, await works just as it would normally. Truly simple.

Of course we can’t use _ but another symbol can be found. Edit: replaced _ with #

The browser console uses $0 for this exact mechanism and bash uses $? to get the last exit code.

@mAAdhaTTah

This comment has been minimized.

Copy link
Contributor

commented Jul 8, 2019

Valid identifiers as the placeholder are not going to advance in committee. See this comment: #91 (comment) and the surrounding thread for background.

@noppa

This comment has been minimized.

Copy link

commented Jul 8, 2019

I think @bfred-it just used _ just as an example and the gist of this suggestion is more about not requiring |> at all, but using the value of the previous statement where <selected placeholder symbol> is found.

Of course we can’t use _ but another symbol can be found.

The said placeholder could just as well be ■ or whatever.
Of course that kind of undermines the

no special syntax

argument a bit.

Kind of related: #118

@jridgewell

This comment has been minimized.

Copy link
Member

commented Jul 8, 2019

This is possible, but I'd worry that it prevents low-level implementation optimizations. If an expression's value is never used (provable when constructing the AST with only parent nodes), the value could have been elided from the actual code. With this, we don't know if the expression value is used until after the AST is constructed (because we're parsing the next sibling of the expression).

@mAAdhaTTah

This comment has been minimized.

Copy link
Contributor

commented Jul 8, 2019

This also sounds super confusing when you can just use variables.

@bfred-it

This comment has been minimized.

Copy link
Author

commented Jul 9, 2019

This also sounds super confusing when you can just use variables.

The whole point of the operator is to avoid intermediary variables; this suggestion enables that without adding another way to call functions (|> fn)

This would also work as an expression:

let newScore = (
  person.score,
  double(#),
  add(7, #),
  boundScore(0, 100, #)
); 
@mAAdhaTTah

This comment has been minimized.

Copy link
Contributor

commented Jul 9, 2019

The whole point of the operator is to avoid intermediary variables

Yeah, I think the operator makes that clearer than a shadow variable, especially if that variable becomes available after every expression. At least with the Smart Pipeline, the placeholder token is only usable within the pipeline, so you have a clear indication as to when it's available and where it's coming from. And the F# version doesn't have these special tokens at all, instead using arrow functions.

@jacobstanley

This comment has been minimized.

Copy link

commented Jul 11, 2019

The look of the functions seems like a separate issue to the pipeline operator.

In F# you'd be able to partially apply the functions, something like this:

let newScore = person.score
  |> double
  |> add 7
  |> boundScore 0 100

If JavaScript had a syntax for partial application then it wouldn't be so ugly. As @mAAdhaTTah points out, this looks to be included in the smart pipelines proposal via #. Partial application would be quite useful in contexts other than just pipelines, so hopefully it gets fleshed out in its own right.

@noppa

This comment has been minimized.

Copy link

commented Jul 11, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.