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

Evaluate left argument before right #2

Open
littledan opened this issue Jul 25, 2017 · 7 comments
Open

Evaluate left argument before right #2

littledan opened this issue Jul 25, 2017 · 7 comments

Comments

@littledan
Copy link

Looking at the source of this module, it seems to transform x |> f directly into f(x). However, any standardized proposal will likely evaluate x first, before f.

@SuperPaintman
Copy link
Owner

So, I don't fully understand what you mean.

Example in a proposal

@littledan
Copy link
Author

For example, if I have

let log = [];
function f() { log.push("f") }
function g() { log.push("g") }
f() |> g()

After that code runs, log should be ["f", "g"].

I don't see examples in the proposal, probably because it doesn't really make sense to write code this way.

@SuperPaintman
Copy link
Owner

SuperPaintman commented Aug 8, 2017

Yeah, that's right. These ways are equivalent:

Calling in calling:

let log = [];
function f() { log.push("f") }
function g() { log.push("g") }

f() |> g

console.log(log) // => ["f", "g"]

Pipeline:

let log = [];
function f() { log.push("f") }
function g() { log.push("g") }

g(f())

console.log(log) // => ["f", "g"]

And, as I understand it, when we use a pipeline, we say:

f()  // Get result of this function
|>   // and put it as a first parameter in to
g    // that function, and call it.

Or did I misunderstand you?

@littledan
Copy link
Author

Sorry, there was a typo in my example, and it seems like you fixed it in a direction that's different from what I was going for. It should be:

function g() { log.push("g"); return () => {} }

Then, the code f() |> g() is related to the code g()(f()).

But! They differ in the evaluation order of f() vs g(). In the pipeline example, f() runs first. But in the normal function call code, g() goes first.

This can be fixed by desugaring into something like this:

let __arg = f();
let __fn = g();
__fn(__arg)

@SuperPaintman
Copy link
Owner

Oh, I understood what you mean, but I didn't find an opinion on this case in proposal.
I think, you'd better ask this question in proposal repo: https://github.com/tc39/proposal-pipeline-operator
Please let me know later what they think about this.

Or my implementation is wrong?

@littledan
Copy link
Author

Well, that proposal doesn't have specific enough spec text to say things one way or another.

I'd be really surprised if it went any other way than left-to-right (and I think it would be shot down in committee if so), but it would be fine with me if you feel like waiting for more solid confirmation first.

@littledan
Copy link
Author

I made a spec text proposal at tc39/proposal-pipeline-operator#51 which evaluates the left argument before the right one. I'd be interested in any feedback you have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants