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

Alternative implementation idea using comma operator. #308

Open
haydenlauer opened this issue Nov 27, 2024 · 7 comments
Open

Alternative implementation idea using comma operator. #308

haydenlauer opened this issue Nov 27, 2024 · 7 comments

Comments

@haydenlauer
Copy link

Instead of making a pipeline operator we make an expression-scoped variable assignment operator, and then use the comma operator to achieve the pipeline functionality. I think this may give us the best of both worlds.

value |>x, foo(32,x)

This lets you name variables like the F# style but also lets you use arbitrary expressions like the hack style. As an added benefit, you can access any previously defined variable like so:

max-min |> range,
pos-min|>pos1,
console.log(pos1,range),
pos1/range*100 |> percent,
percent>50

Also because it uses the existing comma operator, any performance issues or conflicts with other proposals are pre-existing. The only performance hit would be from expression-scoped variables which I would think should be equivalent to this:

(()=>{
let range=max-min;
let pos1=pos-min;
console.log(pos1,range);
let percent=pos1/range*100;
return percent>50})();

Side note: it would be nice if the |> could be used without commas, but that might be harder to implement:

(pos2/(max-min|>range)-pos1/range)
@snatvb
Copy link

snatvb commented Nov 29, 2024

I have no idea reason to have it and we have already , as seporator
for exmaple:

function foo() {
  return (console.log('hi'), 1) 
}

const result = foo() // output console log: 'hi'; result = 1

If you want Do notation, that should be done as another proposal. And we can learn from other functional languages instead of inventing our own

@haydenlauer
Copy link
Author

I have no idea reason to have it and we have already , as seporator for exmaple:

I listed a few benefits in the OP, but I would be happy to answer any specific questions that you have.

Also I am aware of the comma operator already, as I used it several times in the OP as well as explicitly referencing "the existing comma operator."

@Jopie64
Copy link

Jopie64 commented Nov 30, 2024

I'm not sure about the benefits of this proposal, but it got me thinking though... What if... let within an expression with parenthesis was allowed?
Maybe that would be enough 🤔

actionUri
  |> %.tolower()
  |> baseUri + %
  |> await fetch(%)
  |> JSON.parse(%)

Would be similar to

(let $= actionUri
  ,$= $.tolower()
  ,$= baseUri + $
  ,$= await fetch($)
  ,$= JSON.parse($)
)

Not that much more noise if you ask me... Also you can use your own topic token as long as it can be an identifier.

@gustavopch
Copy link

@Jopie64 I think it wouldn't work well with TypeScript as $ would only be able to have a single type, not a different type per step.

@haydenlauer
Copy link
Author

@Jopie64 That is what I'm proposing. I'm just using the |> operator to make the syntax closer to F# and reduce boilerplate.

@VitorLuizC
Copy link

What exactly you mean by "achieving the pipeline functionality"?

I'm asking because value |>x, foo(32,x) doesn't "pipe" value in a sequence of expressions. You're just assigning value to a variable then using it in a expression. It doesn't seem to address current proposal goals.

  • It doesn't solve deep nesting (e.g. a(b(c(d(1)), 3))).

  • It doesn't reduce necessity of creating and naming temporary variables.

@haydenlauer
Copy link
Author

  • It doesn't solve deep nesting (e.g. a(b(c(d(1)), 3))).

This would become d(1) |> c1, c(c1) |> b1 , b(b1,3) |> a1, a(a1).

  • It doesn't reduce necessity of creating and naming temporary variables.

Technically it does not. But, because the variables are scoped to the expression, you could just name them all r1, r2, ...rN. It also allows you to write a(c(7), b(12)) as c(7) |> a1, b(12) |> a2, a(a1, a2) which I would imagine is more readable than the hack or F# equivalent.

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

5 participants