If so I think that should be a consideration, even if in practice there isn't massive value in a backward pipe operator in javascript. It would be a shame to later want to add it and have to add another layer of kludge and messy syntax.
a_value |> (fun a b -> a + b) <| b_value
In F#, this works because of automatic function currying. Not sure how that would apply to Javascript, though. Without function currying, what would this even mean? a_value |> ((a, b) => a + b) <| b_value
...since there's no currying, you'd just immediately invoke the function that sits "in the middle" with `a` set to `a_value`, but `b` set to `undefined`.The Hack-style proposal though...I don't think it would work with a backward-pipe operator at all. Not without adding a _second_ special-case symbol, at least.
The expression you see is evaluated left-to-right. (|>) is a function taking a_value and (fun a b -> a+b) as arguments and applying a_value to the function. This returns a function taking b as an argument (that’s called a partial application). (<|) is once again a function taking the resulting function and b_value as arguments and applying b_value to its first argument which finally returns the wanted results.
The issue with unary function doesn't exist in F# because every functions can be seen as a unary function returning a function taking one less argument thanks to partial application. That's the beauty of currying.
Then there are ||>, |||> and <|, <||. As well as >> and <<.
Would be nice to use computational expressions for async and generators. (https://es.discourse.group/t/add-computation-expressions-fro...).
This proposal reminds me of Scala with anonymous parameters '_'. Could I use more than one '%' for curried functions. xs.reduce(_+_)? Though I guess for perf reasons it might make sense to keep things as tuples so it would be xs.reduce(%[0]+%[1]).
On the other hand, it works well in F# (and the ML that then borrowed it from F#) because it’s just a standard infix operator there and everything is already curried which is definitely not the case with JS. It means you will nearly always have to use a lambda when piping in JS which is honestly a bit tedious.
let firstName =
person
|> fun x -> x.FirstName
|> trim
Some languages allow: let firstName =
person
|> .FirstName
|> trim
But you're right, expression-orientation and currying make this much more natural.You can always give your input on such decisions, the % token is still being "bikeshedded"[2] (is that a word?), and there's still a possibility of making follow-ups proposals that could implement some F#-esque implementation
[1]: https://github.com/tc39/proposal-pipeline-operator/issues/22...
[2]: https://github.com/tc39/proposal-pipeline-operator/issues/91
That's a lot of prior art for people to be comfortable with and as the fat arrow already has another meaning, overloading it in pattern matching might complicate things.
Another syntax for pattern-matching could be simply ':' instead of the arrow.
In the early days of Smalltalk the Smalltalk return statement was simply '^'. That could also be suitable for pattern matching. The idea would be that the switch statement returns something meaning pushing it up from the expression to whoever called it. So '^' might be good for that. Whereas pushing the results to the right to the next expression could be ->.
Just my preferences.
It always seems there’s some obscure edge case that derails the nice path for these spec proposals though. I haven’t tracked the conversation on this one, but wonder why they didn’t go with it.
const weather = `https://api.weather.gov/gridpoints/TOP/31,80/forecast`
|> await fetch(%)
|> await %.json()
|> %.properties.periods[0]
In F# it would be much more verbose, const weather = `https://api.weather.gov/gridpoints/TOP/31,80/forecast`
|> fetch
|> await
|> json
|> await
|> x => x.properties.periods[0]
// or |> { properties : { periods: [result] } } => result
Hack will also requires less key strikes for calling functions that have multiple augments.If your project is going to be robust, you MUST intercept the result and check before asking for JSON.
const resp = fetch(`https://api.weather.gov/gridpoints/TOP/31,80/forecast`)
if (resp.ok) {
const weather = await resp.json()
|> x => x.properties.periods[0]
} else {
...
}
Your hack-syntax response would then become const resp = fetch(`https://api.weather.gov/gridpoints/TOP/31,80/forecast`)
if (resp.ok) {
const weather = await resp.json()
|> %.properties.periods[0]
} else {
...
}
Hardly a big win and not at all a win when you realize that you can't copy/past code to and from that syntax without risking weird errors due to the special symbol.`await` isn't a function and (as I noted) either wouldn't be possible or would require special syntactic consideration for F# syntax, but the hack syntax is basically one giant ball of special syntactic considerations.
I'd rather explicit async/await and keep the simplicity of the F# syntax.
Hack proposal
F# proposal F# proposal would make `await` and `yield` into special syntax cases or not allowed.I'd rather do await/yield the old fashioned way (or slightly complicate the already complex JS syntax rules) than add the weird extra syntax. Arrow functions are elegant and already well-known and well-understood.