Live data from Hacker News

Pipe Operator (|>) For JavaScript

github.com

411–420 of 437 posts

Re: Pipe Operator (|>) For JavaScript

#411
post #407

Earlier quoted context omitted.

Why not use |> for that?

It's more common for pattern matching Java's new syntax, Erlang, Elixir, StandardML (fat arrow), F#, Kotlin, Ocaml, Haskell, Rust (fat arrow), Ada (fat arrow), Scala (fat arrow), Zig (fat arrow), and probably a bunch of others too. 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.

Right but then it would not be the same as in those other languages which use fat arrow anyway.

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.

Re: Pipe Operator (|>) For JavaScript

#412

Earlier quoted context omitted.

> That brings me to something I really want in JS, actual unmutable values. If you use `const x = new SomeClass()`, you cannot reassign it, but you can change fields. The first time I encountered `const`, I thought it did the opposite. It would be cool if you could declare something (object, array) to be an immutable value. That sounds like a fundamental mis-understanding. Variables do not hold objects, they hold ref…

I don't want to freeze the object, I want to have a handle that doesn't allow me to modify the object. (Whether that would be at all possible in JS is another question.) So let foo = {field:1}; immutable let bar = foo; bar.field = 2; // error foo.field = 3; // ok This is what I actually want when I think "const". I don't really care that you can reuse a variable, or re-seat a value. What I care about is that I reciev…

If you are using TypeCript checkout ReadOnly utility type

https://www.typescriptlang.org/docs/handbook/utility-types.h...

Re: Pipe Operator (|>) For JavaScript

#413
post #384
post #52

Earlier quoted context omitted.

TC39 proposals often have rubbish real world examples that should never see the light of day in a JS codebase. It makes me really question the judgement of the people that are working on this language, and it explains how some of the shittier proposals manage to slip in and why the good ones are misused all over the place in every real world codebase when this is the kind of guidance devs have on where to use fancy n…

React component should be chock full of do expressions If the motivation behind these features are React components, javascript clearly lacks proper logic in array (and object) literals, like '[' if (cond) […]expr1 [else […]expr2] ']' Although it seems pretty strange to add syntax only because some specific user interface library together with a specific language extension could benefit from it.

Do you mean this?

    [cond ? expr1 : expr2]
Or maybe this:

    [(() => {/*code*/})()]

Re: Pipe Operator (|>) For JavaScript

#414
post #51

JavaScript isn't that kind of programming language. I don't know why people are constantly trying to make it something else.

There is an increasingly large population of "programmers" that have never known anything other than Javascript and don't understand the concept of "different languages for different purposes". They want Javascript to do everything so they don't have to leave their comfort zone.

"There is an increasingly large population of "programmers" that have never known anything other than Python and don't understand the concept of "different languages for different purposes". They want Python to do everything so they don't have to leave their comfort zone."

Also, s/Javascript/$LANG_YOU_HATE/g

Re: Pipe Operator (|>) For JavaScript

#415

Can't javascript already do [this][1]? const thrush = (value, func, ...funcs) => func ? thrush(func(value), ...funcs) : value; // no new syntax required thrush( envars, Object.entries, x => x.map(([key, val]) => `${key}=${val}`), x => x.join(' '), x => chalk.dim('$ ' + x, 'node', args.join(' ')), console.log); [1]: http://www.davidgoffredo.com/thrush.html

Thrush combinator is neat. Here's another without using recursion:

  const thrush2 = (value, ...funcs) => funcs.reduce((value, func) => func(value), value);

  const thrush3 = (value, ...funcs) => { for (const func of funcs) value = func(value);  return value }

Re: Pipe Operator (|>) For JavaScript

#416
post #287
post #272

Earlier quoted context omitted.

I don't see design docs as a tutorial on how to be a better programmer using a new syntax, the goal is to flesh out a new concept built on some fundamental ideas. You cherry picked one example of a tangled/messy block of code that they used to communicate specific idea around "left to right" comprehension and flow of the data using the new syntax. For that specifically it did a fine job. But that doesn't mean that's…

The way I read the proposal is that they used a real-world example from a commonly used codebase (React) to demonstrate how this feature would improve it. I think that's a good approach as features are intended to address real-world concerns, and concrete real-world examples help with that. I don't think it's fair to say it's "cherry picking" to focus on the example they focus on themselves. They have a few other exa…

Considering it was their opening example (which they reiterated multiple times) I will concede it was a poor choice. Since yes one of the top goals is selling the general idea. So it's fair the general audience would take it at face value, especially when they use it multiple times as a real world use case, it is hard to take it any other way.

Plus the doc is #1 on HN after all, which could IRL push it beyond a proposal stage if done right.

Re: Pipe Operator (|>) For JavaScript

#417

Earlier quoted context omitted.

> That brings me to something I really want in JS, actual unmutable values. If you use `const x = new SomeClass()`, you cannot reassign it, but you can change fields. The first time I encountered `const`, I thought it did the opposite. It would be cool if you could declare something (object, array) to be an immutable value. That sounds like a fundamental mis-understanding. Variables do not hold objects, they hold ref…

I don't want to freeze the object, I want to have a handle that doesn't allow me to modify the object. (Whether that would be at all possible in JS is another question.) So let foo = {field:1}; immutable let bar = foo; bar.field = 2; // error foo.field = 3; // ok This is what I actually want when I think "const". I don't really care that you can reuse a variable, or re-seat a value. What I care about is that I reciev…

I agree that having a "can't modify this object" via this reference is useful and that JavaScript doesn't have it. TypeScript has it with `ReadOnly`

It could be worse. You could be in python that has no const whatsoever :P

I also agree pass by reference is useful. JavaScript only has pass by value, similar to python.

Re: Pipe Operator (|>) For JavaScript

#418
Well here’s a solution to a problem that didn’t really need any solving.

How about spending time on:

• Actors • Real Immutable Structs

?

This is going to create more code unreadable, while it may be cool and clever to put a pipe in and call it a day, I imagine most production code will be a pile of nested pipe gibberish that any junior engineer or new hire would pull their hair at trying to piece together.

We want to create features, and solve business problems and that in turn also requires maintenance. This is just some clever macro wrapper around currying. While functional style is cool and all I don’t really see much value for most day to day workflows

Re: Pipe Operator (|>) For JavaScript

#419
post #384

Earlier quoted context omitted.

React component should be chock full of do expressions If the motivation behind these features are React components, javascript clearly lacks proper logic in array (and object) literals, like '[' if (cond) […]expr1 [else […]expr2] ']' Although it seems pretty strange to add syntax only because some specific user interface library together with a specific language extension could benefit from it.

Do you mean this? [cond ? expr1 : expr2] Or maybe this: [(() => {/*code*/})()]

More like this:

  [...(cond ? [expr1] : [])]

Re: Pipe Operator (|>) For JavaScript

#420
post #381

Earlier quoted context omitted.

How so? JS already has operators, but placeholders is a totally new concept

Is there another JavaScript feature that looks like a list of function names but which causes actions to be performed? Or operators that operate on both functions and values? The placeholders are very similar to variables, so do what they look like they do, and it's only the implicit definition that is new. The explicit placeholders feel much more like JavaScript to me than the implicit function calling of the F# pro…

- functions and lambdas are already first-class values in JS

- we already have operators on values, such as +

- |> is just another operator on values

Post reply on HN