Live data from Hacker News

The JavaScript Pipeline Operator

yanis.blog

21–30 of 63 posts

Re: The JavaScript Pipeline Operator

#21
post #14
post #4

ES6 was needed, but I think js should hit the breaks for new language features for a while.

I tend to agree with you. The thing is that most people would agree but at the time everyone wants different features. Me personally I even think we could be better without classes and all that "pretend you're writing Java" nonsense

> I even think we could be better without classes

Absolutely. Classes are also a big part of why there's still a lot of bikeshedding for some new features (class properties, decorators, private fields, etc).

Classes are familiar to most developers these days, but they're an absurdly complicated construct. The current implementation in javascript is "maximally minimal", in that the TC39 pushed the minimum they could that was still kind of sortoff useful to get the wheel rolling...but it's still not good enough. They'll need several more updates before they're truly useful. And even when that's all there, they still won't offer much that wasn't already doable, arguably better if less familiar.

At the end of the day, ES6 classes were designed back in the days where OOP was getting big in JavaScript. It took so long to release that by the time they were, the field had changed a lot.

Re: The JavaScript Pipeline Operator

#22

Been using this in LiveScript for awhile (ls also provides it in the opposite direction <| ). Very useful for chaining functions in a logical order and without unnecessary () nesting. Absolutely needs to be paired with partial application though for multiple argument functions.

Yeah, partial application is also a stage 1 proposal now. But for now I guess you could also do with auto-curring functions (https://ramdajs.com/ or something like that)

Re: The JavaScript Pipeline Operator

#23
You could just use ramda https://ramdajs.com/docs/#pipe

Not quite as seamless and it would be nice to have |> but ultimatles introduces a lot of complexity.

The only good reason I could see is if there is a performance benefit a JavaScript JIT could take advantage of by having the |> operator?

Re: The JavaScript Pipeline Operator

#25

FWIW, several language communities call this "the Thrush combinator"[0]. Armed with that term, searching the web for background/info/theory on this topic will be easier. 0 - https://debasishg.blogspot.com/2009/09/thrush-combinator-in-...

Thanks! Sounds a bit too scientific to my ear but good to know

Re: The JavaScript Pipeline Operator

#26

You could just use ramda https://ramdajs.com/docs/#pipe Not quite as seamless and it would be nice to have |> but ultimatles introduces a lot of complexity. The only good reason I could see is if there is a performance benefit a JavaScript JIT could take advantage of by having the |> operator?

Honestly, I don't think so. More likely this will be just a syntactical sugar.

Re: The JavaScript Pipeline Operator

#27
post #13
post #11

Earlier quoted context omitted.

Please explain.

64 |> Math.sqrt becomes pipe(64, Math.sqrt) 64 |> Math.sqrt |> Math.sin becomes pipe(pipe(64, Math.sqrt), Math.sin) Assuming pipe was smart and took varags we can make this better. 64 |> Math.sqrt |> Math.sin becomes pipe(64, Math.sqrt, Math.sin)

A pipe operator compiles directly into the AST and completely disappears before execution.

    64 |> Math.sqrt |> Math.sin
    Compiles in the AST to
    Math.sin(Math.sqrt(64))

    //curries variables away from functions
    var realPipe = (a, ...args) => (...initParams) => {
      var acc = a(...initParams);
      for (var i = 0; i  
      (typeof a === "function")
        ? realPipe(a, ...args)
        : realPipe(...args)(a)
I'd note that this pipe implementation also bleeds its abstraction. JS has first-class functions, but this will never treat a function as data. You must be aware of how it works so if you are passing a function (for example, a getter function), you must manually curry it afterward.

This complication is why most pipe implementations don't actually mess with that data attribute part (they just skip to the realPipe implementation), but since the pipe operator can handle it, I figured I'd try to as well to show the issues.

Re: The JavaScript Pipeline Operator

#28
You don’t need lodash or a new operator for the example provided:

  departments
    .flatMap(d => d.employees)
    .map(e => e.salary)
    .reduce((p, v) => Math.max(p, v), 0)
That said, the pipe operator is nice because it allows similar chaining patterns on subjects that are not arrays, as shown in the proposal’s README: https://github.com/tc39/proposal-pipeline-operator/blob/mast...

Re: The JavaScript Pipeline Operator

#29
post #12

Earlier quoted context omitted.

Your example falls apart when making multiple function calls.

Not quite sure what you mean. But how do you chain multiple functions?

With the way syntax for function calls works, the innermost thing is executed first, and you end up reading stuff from right to left:

   third(z, second(y, first(x)))
With the pipeline operator (and assuming a language that allows partial application), the order in which things execute matches the order in which you read it:

  first x |> second y |> third z

The benefits start getting more noticeable when you're talking about stuff like manipulating sets of data. For example:

  myList
  |> filter (x -> x.color == 'red')
  |> sortBy (x -> x.count)
  |> first
compared to these sorts of pyramids of doom:

  first(
    sortBy(
      x -> x.count,
      filter(
        x -> x.color == 'red',
        myList
      )
    )
  )

Re: The JavaScript Pipeline Operator

#30

You don’t need lodash or a new operator for the example provided: departments .flatMap(d => d.employees) .map(e => e.salary) .reduce((p, v) => Math.max(p, v), 0) That said, the pipe operator is nice because it allows similar chaining patterns on subjects that are not arrays, as shown in the proposal’s README: https://github.com/tc39/proposal-pipeline-operator/blob/mast...

Good point. Is flatMap already supported by browsers or is it a proposal?
Post reply on HN