Live data from Hacker News

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

github.com

81–90 of 148 posts

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

#81
post #22

Earlier quoted context omitted.

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

It feels a bit unnatural to pipe multiple arguments like that in JS, and you could inline them in the first call without losing legibility: a = c(b, 7) |> d(%) It's the arguments added way down the line that are problematic: a = f((e(d(c(b))), 4), 5) a = ((b ~> c ~> d), 4 ~> e), 5 ~> f a = b |> c(%) |> d(%) |> e(%, 4) |> f(%, 5) Smart pipes [1] were a bit nicer about that: a = b |> c |> d |> e(#, 4) |> f(#, 5) Basica…

Aside, but there's another kind of syntax possible here:

    const result = (
      await fetch(url)
        .json()
        ::Object.keys
        .map(key => ...)
        ::new ByteArray
    );

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

#83

export const pipe = (f: Fun ) => { return { to: (g: Fun ) => pipe((arg: A) => g(f(arg))), build: () => f, }; }; Much simpler alternative. const process = pipe(readLines).to(x => cut(x, 2)).to(....).build() const result = await process(fd) P.S the Fun type is just (...args: A) => B

This solution seems a lot cleaner to me syntactically, but it does lack helpers like .concat(). You'd have to write methods like .pipe( x => x + "!") to extend methods. Then again, prefixing and postfixing operaties shouldn't be too hard to write either

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

#84

How's it different from other `pipe` implementations around, e.g. the one from `fp-ts`? https://gcanti.github.io/fp-ts/modules/function.ts.html#pipe

The pipe function of `fp-ts` doesn’t have direct access to methods and properties of the pipe output like Verticalize. You have to wrap them into anonymous functions. Same with promises.

You can always

   pipe(
     2,
     double,
     x => x.toString()
   )


I love your approach too, it's really more of a pipe operator than a `pipe` function which expects the pipeline to happen on the arguments side.

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

#87

Earlier quoted context omitted.

The strength of the pipeline operators comes when you have to do method chaining things like: const foo = capitalize(underline(reverse(exclaim(indent(value))))); I already forgot the amount of parentheses I needed to close with while typing that.

value.indent() .exclaim() .reverse() .underline() .capitalize() There’s always the OOP version of method chaining!

This won't look as good when you include all the boilerplate which defines those methods, and which needs to be repeated or otherwise included for every "value" type. You don't have to worry about this when they are pure utility functions.

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

#88
post #40

Earlier quoted context omitted.

In this case the variable is the documentation of the code. You don't have to write something like "this is the message", you can see it.

What if it isn't a message? What if "msg" is already used for another variable?

"_msg", then.

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

#90

>The following example code is a bit hard to read: >const { status } = await send(capitalize(greeting) + "!") >console.log(status) No it's not hard to read. >Make it less nested, more vertical, by using the V "pipe": >V( greeting, // initial value "hi" >V (capitalize), // custom function call "Hi" >V .concat("!"), // String method `concat` call "Hi!" >V (send), // custom async function call Promise { } >V .status, //…

Is functional programming a joke? YMMV...
Post reply on HN