Live data from Hacker News

Working pipe operator today in pure JavaScript

github.com

81–90 of 109 posts

Re: Working pipe operator today in pure JavaScript

#81

Earlier quoted context omitted.

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.

Thank you for clarifying. (I think?)

I think what confused me is the passive language: "everything gets converted" sounds (to me) like the runtime or some aspect of language semantics is converting everything, rather than developers. Whereas this is the same complaint I mentioned.

Re: Working pipe operator today in pure JavaScript

#82
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

Not if you consider that the linked repo requires you to use asPipe on all functions first. So it's this:

  const greeting = thrush(
    'hello',
    s => s.toUpperCase(),
    s => s + '!!!'
  );
Vs this:

  const upper = asPipe(s => s.toUpperCase())
  const ex = asPipe((s) => s + '!!!')
  const greeting = pipe('hello')
    | upper
    | ex
  await greeting.run()
(And that doesn't work in reality, as the top comment here notes)

Re: Working pipe operator today in pure JavaScript

#84
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!!!"

Thanks for pointing this out, I updated the examples now to this syntax.

Re: Working pipe operator today in pure JavaScript

#85
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.

I wrote this with an LLM but manually changed the README. Thanks for pointing this out, it is now updated.

Re: Working pipe operator today in pure JavaScript

#88
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.

No way dude, this does a disservice to the insanity that is C++'s syntax. Wake me up when you have 6 different initialization syntaxes or fun things like 4[array]

Re: Working pipe operator today in pure JavaScript

#89
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.

Well, Proxy objects do allow you to override the behavior of any property, including Symbol properties. Symbol.iterator is pretty widely used to create custom iterable objects, so I would expect curious devs to have taken a look at what else can be done through the use of Symbol properties.

Re: Working pipe operator today in pure JavaScript

#90

Earlier quoted context omitted.

This isn’t overloading the operator, it is replacing the implementation of type coercion when | is used with pipe() or asPipe() objects. | itself still works exactly as before.

Oh wait, looked at the source again, so it's some weird stateful collection thing triggered by the type coercion? By now I'm wishing that it was operator overloading.

Imagine the possibilities for control flow obfuscation when this trick is used with parentheses wrapping a part of the pipeline. :-)
Post reply on HN