Live data from Hacker News

Show HN: A JavaScript function that looks and behaves like a pipe operator

github.com

11–20 of 148 posts

Re: Show HN: A JavaScript function that looks and behaves like a pipe operator

#11
Honestly, if prototype pollution was not a problem for optimization and other things I’d just toss a

    Object.prototype.pipeTo = function(f) {
      return f(this)
    };
And call it a day. It does about 80% of what a pipe macro would do, has instant compatibility with other prototype methods and leads to very simple types. The only issue is that you have to define lambdas to call multivariable functions. D has got this very, very right.

Re: Show HN: A JavaScript function that looks and behaves like a pipe operator

#13

The following example code is a bit hard to read: const { status } = await send(capitalize(greeting) + "!") console.log(status) I disagree, I find this example code very easy to read because it reads like idiomatic Javascript. Unlike this library.

I think the implication is that it's difficult to read because it's read non-linearly. You first need to start in the middle at `greeting`, then work your way out to `capitalize` on the left, then to `+ "!"` on the right, then work your way out one more layer to the left with `send`.

Some kind of pipe operator can make this easier to read by arranging the operations linearly from left to right (or top to bottom as is the case with this library). Although in my opinion this library has its own readability issues.

Re: Show HN: A JavaScript function that looks and behaves like a pipe operator

#14
This would be my preferred syntax:

    status = greeting+"!" ~> capitalize ~> send
This would need 2 changes to JS:

1: ~> being a pipe operator

2: Calling an async function from within an async function implies await

Without 2, it would look like this:

    status = greeting+"!" ~> capitalize ~> await send

Re: Show HN: A JavaScript function that looks and behaves like a pipe operator

#15
post #14

This would be my preferred syntax: status = greeting+"!" ~> capitalize ~> send This would need 2 changes to JS: 1: ~> being a pipe operator 2: Calling an async function from within an async function implies await Without 2, it would look like this: status = greeting+"!" ~> capitalize ~> await send

There is a proposal to add the pipeline operator |> to the standard that works similar to what you have here.

https://github.com/tc39/proposal-pipeline-operator

Re: Show HN: A JavaScript function that looks and behaves like a pipe operator

#16
post #14

This would be my preferred syntax: status = greeting+"!" ~> capitalize ~> send This would need 2 changes to JS: 1: ~> being a pipe operator 2: Calling an async function from within an async function implies await Without 2, it would look like this: status = greeting+"!" ~> capitalize ~> await send

Which is basically what the proposal is https://github.com/tc39/proposal-pipeline-operator#this-prop...

Re: Show HN: A JavaScript function that looks and behaves like a pipe operator

#17

The following example code is a bit hard to read: const { status } = await send(capitalize(greeting) + "!") console.log(status) I disagree, I find this example code very easy to read because it reads like idiomatic Javascript. Unlike this library.

Right - because this library tries to fake a new language syntax feature without the actual support for it, it ends up being the worst of both worlds. Even ignoring the lines-of-code explosion, the non-standard use of parens/spacing makes it incredible difficult for me to parse.

Props to the author for trying out something neat, but this is probably a bit to clever for my liking.

Re: Show HN: A JavaScript function that looks and behaves like a pipe operator

#18
post #5

Its all a problem of formatting, for me this is simple to read. const { status } = await send( capitalize(greeting) + "!" ) But i would not concat things in a method call, bad style. const message = capitalize(greeting) + "!" const { status } = await send(message) The example from the page is a little bit too simple. With complex operations a pipe-method would make more sense. But i will wait for the native pipeline-…

I think the use of template strings simplifies it, even inline:

const { status } = await send(`${capitalize(greeting)}!`)

console.log(status)

It does reduce the number of discrete operations to make the pipe example look more impressive, though.

Re: Show HN: A JavaScript function that looks and behaves like a pipe operator

#20
post #11

Honestly, if prototype pollution was not a problem for optimization and other things I’d just toss a Object.prototype.pipeTo = function(f) { return f(this) }; And call it a day. It does about 80% of what a pipe macro would do, has instant compatibility with other prototype methods and leads to very simple types. The only issue is that you have to define lambdas to call multivariable functions. D has got this very, ve…

Nowadays, as someone writing a library, you can work around prototype pollution via Symbols and let your users modify prototypes on their terms.
Post reply on HN