Live data from Hacker News

Working pipe operator today in pure JavaScript

github.com

71–80 of 109 posts

Re: Working pipe operator today in pure JavaScript

#71
post #63

Earlier quoted context omitted.

It is not a surprise that overriding the implementation of an operator’s type coercion works and overrides the behavior of the operator’s type coercion.

Do you really think that most JavaScript users are aware that “overriding the implementation of an operator’s type coercion” is a language feature? Sure, you can claim that everyone should know this obscure feature when they don’t. But that’s how this language enters C++ territory.

I actually don't think you are wrong, but I'm not backing that up with any actual data.

I happened to know it because of how the hyperHTML micro-library works; the author went into great detail about it and a ton of other topics. But my gut would say that the average js dev doesn't know about it.

But then... it's useful for creating component frameworks which... most js devs use. Which doesn't mean they know how they work under the hood. But... a lot of devs I've met specifically choose a framework because of how it works under the hood.

... so... I really have no idea how many people know this. I'm still betting it's less than average.

Re: Working pipe operator today in pure JavaScript

#72
post #58
post #10

First example doesn't work though: const greeting = pipe('hello') | upper | ex('!!!') await greeting.run() // → "HELLO!!!" If you look at the tests file, it needs to be written like this to make it work: let greeting; (greeting = pipe('hello')) | upper | ex('!!!'); await greeting.run(); Which is not anymore as ergonomic.

I suspect this was written with an LLM and the author didn't actually verify that the examples in the README worked.

Recently, I ripped usage examples out of a rust project's README.md, and put them in doc comments. Almost all of them were broken due to small changes over time, and I never remembered to update the readme. `cargo test` runs doc comments like mini integration tests, so now the examples never rot. I wish more languages and tools had this feature.

It means having to go to the linked docs (which are automatically pushed to the repo's github pages) to see examples, but I think this is a reasonable tradeoff.

Re: Working pipe operator today in pure JavaScript

#73
post #48
post #8

Further proof that JavaScript accidentally became the new C++. “Aren’t you surprised that this syntax works?” is not praise for a language design.

Is there a language that can’t be contorted in surprising ways that I’m unaware of?

I think that depends on the person, the language, and how familiar they are with the language. Someone's "what the fuck" is another's "obviously it can do that".

Re: Working pipe operator today in pure JavaScript

#74

Earlier quoted context omitted.

`map` returns an array and can only be called on an array.

Right, but I’m not clear on what gets converted to an array. Do you mean more or less what I said in my previous comment? That it requires you (your code, or calling code in general) to perform that conversion excessively?

People write a lot of stuff like [...iterable].map(fn). They do it so much it's as if they do it each time a hat drops.

Re: Working pipe operator today in pure JavaScript

#75

Earlier quoted context omitted.

First of all, with the actual proposal, wouldnt it actually be like this? with the %. xs |> map(%, x => x * 2) |> filter(%, x => x > 4) |> sorted(%) |> take(%, 5); Anything that can currently just chain functions seems like a terrible example because this is perfectly fine: xs.map(x => x * 2) .filter(x => x > 4) .sorted() .take(5) Not just fine but much better. No new operators required and less verbose. Just strictl…

There is more than one proposal; the F#-style one doesn't have the (weird) placeholder syntax. > You can still make it work by adding it to the prototype This is exactly what we want to avoid!

wrap the object?

Why would you want to avoid that? It's controversial syntactic sugar. Enforcing a convention locally seems ideal.

Re: Working pipe operator today in pure JavaScript

#76
post #41
post #20

Earlier quoted context omitted.

Agreed!!!! Serious q: but how does this sentiment change with LLMs? They can pickup new syntax pretty fast, then use fewer tokens...

I’ve heard it said before on HN that this is not true in general because more tokens in familiar patterns helps the model understand what it’s doing (vs. very terse and novel syntax). Otherwise LLMs would excel at writing APL and similar languages, but seems like that’s not the case.

probably because there arent enough apl examples to imbue the rare weird apl tokens with sufficient semantic meaning to be useful.

Re: Working pipe operator today in pure JavaScript

#77
post #10

First example doesn't work though: const greeting = pipe('hello') | upper | ex('!!!') await greeting.run() // → "HELLO!!!" If you look at the tests file, it needs to be written like this to make it work: let greeting; (greeting = pipe('hello')) | upper | ex('!!!'); await greeting.run(); Which is not anymore as ergonomic.

I was playing with it, and you can do this, which looks a little better.

    const greeting = pipe('hello');
    greeting | upper | ex('!!!');
    await greeting.run(); // → "HELLO!!!"
Since it uses the "Symbol.toPrimitive" method, you can use any operator (not just "bitwise OR" (|)).

    const greeting = pipe('hello');
    greeting / upper * ex('!!!');
    await greeting.run(); // → "HELLO!!!"

Re: Working pipe operator today in pure JavaScript

#79
post #48
post #8

Further proof that JavaScript accidentally became the new C++. “Aren’t you surprised that this syntax works?” is not praise for a language design.

Is there a language that can’t be contorted in surprising ways that I’m unaware of?

Swift, IMHO. Grew up on ObjC and the absolutely crazy things you could pull off dynamically at runtime. You can definitely feel they did not want that in Swift. There's operator overriding but idk if I'd count that as contorting in surprising ways shrugs

Re: Working pipe operator today in pure JavaScript

#80
post #49

Neat, but I think that functions already do what we need. For one thing, the example isn't the most compelling, because you can: const greeting = 'hello'.toUpperCase() + '!!!'; or const greeting = 'HELLO!!!'; That said, there is already: function thrush(initial, ...funcs) { return funcs.reduce( (current, func) => func(current), initial); } const greeting = thrush('hello', s => s.toUpperCase(), s => s + '!!!');

Whatever that thrush thing is feels 10x more gross than the pipe

Thrush is the "T combinator" - I believe that the "Thrush" name comes from To Mock a Mockingbird by Raymond Smullyan [1].

[1]: https://www.amazon.com/Mock-Mockingbird-Other-Logic-Puzzles/...

[2]: https://en.wikipedia.org/wiki/Combinatory_logic#In_computing

[3]: https://leanpub.com/combinators/read#leanpub-auto-the-thrush

Post reply on HN