Has anyone suggested a pipe operator for javascript?

Many languages ​​have an operator that allows you to transfer the results of one operation to a call to another (for example, the | operator in bash, the |> operator in F #).

One of the greatest advantages of my mind in spreading the idiom of method binding in javascript is that it reads from top to bottom, from left to right:

 var fooOddSquares = [1, 2, 3, 4, 5] .filter(x => x % 2) .map(x => "foo" + x * x) .reduce(((acc, str, i) => acc[i + 1] = str; return acc), {}); // => {1: "foo1", 2: "foo9", 3: "foo25"} 

compared to composite code:

 var something = func5( func4( func3( func2( func1( somedata ) ) ) ) ); 

which is read from right to left, from bottom to top. I understand that this can be cleared with a functional composition, but this is not necessary. To be absolutely clear what I'm looking for:

 var something = func1(somedata) |> func2 |> func3 |> func4 //etc... 

Performing a google search on a pipe statement in javascript basically provides information about the bitwise OR operation. However, with some digging, I was able to dig up this article describing a dirty hacked version of operator overload that could implement some of what I'm talking about. I also found this one that describes the statement mentioned and says that "it was suggested for javascript".

Looking at ES 2016, I see offers for the exponential operator and the binding operator. Both are useful, but not what I want. So, in the heading at the base, did anyone really suggest this for javascript?

+7
source share
3 answers

This is a github repo and my favorite problem is in it ;) discuss exactly that.

The proposal has been promoted in a small space of ideas for several months, but it very conveniently combines -> and :: with sugar near Function.prototype.apply and Function.prototype.bind .

The current draft for :: is between the scope and the function ( instance::method ) and acts as instance.method.bind(instance) , blocking the scope of this function for any calls. Along with this, -> can be defined to pass an area and an array of arguments (as apply ), therefore instance->method(foo, bar) will desugar up to instance.method.apply(instance, [foo, bar]) . At least, this is one of the areas that are being discussed (disclosure: what I undertake).

+2
source

In December 2015, a conveyor operator was proposed for ES7 (ES2016).

https://github.com/mindeavor/es-pipeline-operator

+14
source

As already mentioned, a conveyor operator was proposed for ES7 (2016), but it doesn’t help much if you want to use something similar right now with babel, which is exactly how I came across this issue after 9 months.

The biggest delay in es-pipeline-operator suggestion support through babel, as far as I know, is the current inability to use |> or <| like operators that create syntax errors and cannot be fixed without changes in babel and, unfortunately, it does not look like the problem will be solved in the near future.

I personally would like to see that the feedback operator in the channel is added to the proposal, as both forward and backward are useful in different situations.

For example, I use the return channel operator when changing functions, or anywhere I would normally use "compose" over "pipe", which I prefer in certain situations for readability.

 const something = curry <| function(state, pattern) { // something } const something = function(state, pattern) { // something } |> curry 

Because these pipe operators are incredibly useful in functional style javascript programming, and in the interest of anyone who came to look for a solution to use it right now, like me, I created a babel plugin that uses bitwise operators << and >> , which I rarely I use the "no pipe"; directive to achieve both direct and return pipelines at present, and in rare cases where bitwise operators are required "no pipe"; will disable the plugin for a specific area.

https://github.com/michaelmitchell/babel-plugin-pipe-composition

+5
source

All Articles