Live data from Hacker News

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

github.com

141–148 of 148 posts

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

#141
post #135

Earlier quoted context omitted.

This: a = f((e(d(c(b))), 4), 5) Seems not to make sense. Replacing c(b) with x we get: a = f((e(d(x)), 4), 5) Replacing d(x) with x we get: a = f((e(x), 4), 5) Replacing e(x) with x we get: a = f((x, 4), 5) What is that?

Sorry, messed up parens here. It should have been: a = f(e(d(c(b))), 4), 5) b is passed to c, then the result is passed to d, then to e along with 4 as a second parameter, and finally to f along with 5.

    a = f(e(d(c(b))), 4), 5)
That has 4 opening parenthesis and 5 closing parenthesis.

    b is passed to c

        b ~> c

    then the result is passed to d

        b ~> c ~> d

    then to e along with 4 as a second parameter

        b ~> c ~> d,4 ~> e

    and finally to f along with 5

        b ~> c ~> d,4 ~> e,5 ~> f

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

#142
post #141

Earlier quoted context omitted.

Sorry, messed up parens here. It should have been: a = f(e(d(c(b))), 4), 5) b is passed to c, then the result is passed to d, then to e along with 4 as a second parameter, and finally to f along with 5.

a = f(e(d(c(b))), 4), 5) That has 4 opening parenthesis and 5 closing parenthesis. b is passed to c b ~> c then the result is passed to d b ~> c ~> d then to e along with 4 as a second parameter b ~> c ~> d,4 ~> e and finally to f along with 5 b ~> c ~> d,4 ~> e,5 ~> f

    b ~> c ~> d,4 ~> e
This reads as

    b ~> c ~> (d, 4) ~> e
to me. Sure, you can get used to it, but it’s pretty unnatural. Is this syntax used in any other languages?

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

#143
post #44

Lisper screaming See Clojure macros for thread-first `->`, thread-last `->>`, thread-as `as->`, `some->`, `some->>` and `cond->`: https://clojure.org/guides/threading_macros You can leave all this hurt behind you and use ClojureScript, which compiles to JavaScript.

Example:

    (->> (range 10)  ;; (0 1 2 3 4 5 6 7 8 9)
      (filter even?) ;; (0 2 4 6 8)
      (map inc)      ;; (1 3 5 7 9)
      (apply +))     ;; 25
     => 25
which macroexpands to:

    (apply + (map inc (filter even? (range 10))))

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

#144
post #108

Earlier quoted context omitted.

Ok, you aren't arguing for 'pure' functional programming, but I think you have sidestepped my question, then: The pipe operator is just one part of functional programming, composing and applying functions `f(g(x))` is still functional, and I would argue it exactly what is happening in the first example.

Nobody said the first example _isn't_ functional. The point is that function composition f(g(x)) is very common in a functional paradigm and many functional languages have added a Pipeline operator as syntactic sugar for this use case.

Nobody explicitly said it wasn't functional, but the original question was "why would you do b (the 'V' shit), instead of a?" ('is this a joke?'), and the response was "well if you care about functional programming you would!" ('is functional programming a joke?'), which is heavily implying that the first example is not functional! (or 'less' functional)

That you don't recognise that is strange to me, and if after my explanation you can't understand that, I don't know what to tell you.

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

#145
post #108

Earlier quoted context omitted.

Nobody said the first example _isn't_ functional. The point is that function composition f(g(x)) is very common in a functional paradigm and many functional languages have added a Pipeline operator as syntactic sugar for this use case.

Nobody explicitly said it wasn't functional, but the original question was "why would you do b (the 'V' shit), instead of a?" ('is this a joke?'), and the response was "well if you care about functional programming you would!" ('is functional programming a joke?'), which is heavily implying that the first example is not functional! (or 'less' functional) That you don't recognise that is strange to me, and if after my…

If you can’t understand the difference between pure (no side effects) and functional (applying and composing functions), I don’t know what to tell you.

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

#146

Earlier quoted context omitted.

Nobody explicitly said it wasn't functional, but the original question was "why would you do b (the 'V' shit), instead of a?" ('is this a joke?'), and the response was "well if you care about functional programming you would!" ('is functional programming a joke?'), which is heavily implying that the first example is not functional! (or 'less' functional) That you don't recognise that is strange to me, and if after my…

If you can’t understand the difference between pure (no side effects) and functional (applying and composing functions), I don’t know what to tell you.

English isn't your first language, so I understand your confusion throughout this thread.

It's not a problem, don't worry about it you will get there. Enjoy your day!

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

#147
post #141

Earlier quoted context omitted.

a = f(e(d(c(b))), 4), 5) That has 4 opening parenthesis and 5 closing parenthesis. b is passed to c b ~> c then the result is passed to d b ~> c ~> d then to e along with 4 as a second parameter b ~> c ~> d,4 ~> e and finally to f along with 5 b ~> c ~> d,4 ~> e,5 ~> f

b ~> c ~> d,4 ~> e This reads as b ~> c ~> (d, 4) ~> e to me. Sure, you can get used to it, but it’s pretty unnatural. Is this syntax used in any other languages?

The nice aspect of "this ~> that" is that the meaning is simply "put this into that". And that it results in the shortest code.

An alternative would be:

    b ~> c ~> d ~> e(%,4)
Slightly longer, but maybe easier to read. Also easier to handle, as you can remove the fourth part " ~> e(%,4)" without having to change the third part.

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

#148

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

I like

https://github.com/WiseLibs/wise-river

For complex uses

Post reply on HN