Live data from Hacker News

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

github.com

21–30 of 148 posts

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

#21

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.

Which supports the age-old notion that readability is in the eyes of the reader.

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

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

The difference is that they want to turn

    a = d(c(b,7))
into

    a = b |> c(%,7) |> d(%)
and I would like to see it turn into

    a = b,7 ~> c ~> d

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

#23
I mean, if you abuse formatting enough I suppose... although I will say, the pipeline/bind operator has been in proposal for a decade at this point.. it's depressing that some version hasn't shipped, because C# extensions and similar in swift can really help shift how from what.. it* could really help JavaScript a lot

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

#24
I use ppipe: https://github.com/egeozcan/ppipe

Looks tidier to my eyes

    const newPipe = ppipe.extend({
      divide (x, y) {
        return x / y;
      },
      log(...params) {
        console.log(...params);
        return params[params.length - 1];
      }
    });

    const res = await newPipe(10)
      .pipe(x => x + 1)
      .divide(_, 11)
      .log("here is our x: ") //logs "here is our x: 1"
      .pipe(x => x + 1) // 2

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

#27
I like goofy projects like this one but I think that if you insist on having calls there, then might as well make it more explicit with something like this:

  pipe((p) => ([
    "hello",
    p.concat("!"),
    send,
    p.status,
    console.log
  ]));
Where `p` stands for Proxy to the previous result. Under the hood it would just return an object describing the name of the called method and arguments passed.

The pipe function would then iterate over the array, calling methods as described.

It's not immediately clear what should happen when a method's return type is a function, but I suppose this can be handled via convention.

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

#28
post #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.

Would that mean that instead of obj.pipeTo() I'd have to call obj[PIPE_TO] or something like that?

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

#29
post #22

Earlier quoted context omitted.

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

The difference is that they want to turn a = d(c(b,7)) into a = b |> c(%,7) |> d(%) and I would like to see it turn into a = b,7 ~> c ~> d

Does JS have a splat operator? So:

  a = (b, 7) |> c(*%) |> d(%)
Too much operator soup I guess.

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

#30

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.

If you read all the text, it seems like the entire repo is intended to be a satire on https://github.com/tc39/proposal-pipeline-operator.
Post reply on HN