TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Pipe Operator (|>) For JavaScript

309 pointsby nassimsoftwareover 2 years ago

64 comments

arp242over 2 years ago
Is this:<p><pre><code> Object.keys(envars) .map(envar =&gt; `${envar}=${envars[envar]}`) .join(&#x27; &#x27;) |&gt; `$ ${%}` |&gt; chalk.dim(%, &#x27;node&#x27;, args.join(&#x27; &#x27;)) |&gt; console.log(%); </code></pre> Really better than:<p><pre><code> console.log(chalk.dim( `$ ${Object.keys(envars) .map(envar =&gt; `${envar}=${envars[envar]}`) .join(&#x27; &#x27;) }`, &#x27;node&#x27;, args.join(&#x27; &#x27;) )); </code></pre> That&#x27;s the real-world example they have (I reformatted the second one slightly, because it looks better to me). Neither seems very good to me, and the |&gt; version doesn&#x27;t really seem &quot;less bad&quot;.<p>Can also write it as:<p><pre><code> process.stdout.write(chalk.dim( `$ ${Object.keys(envars) .map(e =&gt; `${envar}=${e[envar]}`) .join(&#x27; &#x27;) }`, )) console.log(chalk.dim(&#x27;node&#x27;, args.join(&#x27; &#x27;))) </code></pre> Which seems clearer than either because it splits out &quot;print env variables&quot; and &quot;print out node args&quot;. And it would be even better with some sort of helper to convert an object to k=v string:<p><pre><code> console.log(chalk.dim(`$ ${dumpObj(envars)}`, &#x27;node&#x27;, args.join(&#x27; &#x27;))) </code></pre> ---<p>I also feel this:<p>&gt; In the State of JS 2020 survey, the fourth top answer to “What do you feel is currently missing from JavaScript?” was a pipe operator.<p>Is the wrong way to go about language design. Everyone wants something different, and if you just implement the &quot;top 5 most requested features&quot; you&#x27;re going to end up with some frankenbeast of a language.
评论 #34455548 未加载
评论 #34455349 未加载
评论 #34454868 未加载
评论 #34454758 未加载
评论 #34455890 未加载
评论 #34456080 未加载
评论 #34455430 未加载
评论 #34454895 未加载
评论 #34455045 未加载
评论 #34454926 未加载
评论 #34455088 未加载
评论 #34456810 未加载
评论 #34457106 未加载
评论 #34514331 未加载
评论 #34455210 未加载
评论 #34458285 未加载
评论 #34455027 未加载
评论 #34461200 未加载
评论 #34457723 未加载
评论 #34455282 未加载
评论 #34456404 未加载
评论 #34455125 未加载
评论 #34458477 未加载
评论 #34455229 未加载
评论 #34456185 未加载
评论 #34457883 未加载
评论 #34459945 未加载
评论 #34457458 未加载
评论 #34455404 未加载
评论 #34460995 未加载
评论 #34457880 未加载
评论 #34454821 未加载
kriz9over 2 years ago
Temporary variables are often tedious? I have found that well named temporary variables are the only clear way to comment code without actually writing the comment. The version with temporary variables is much easier to understand without having to read the rest of the code.
评论 #34455584 未加载
评论 #34455459 未加载
评论 #34455532 未加载
评论 #34455882 未加载
评论 #34459946 未加载
评论 #34456112 未加载
评论 #34455993 未加载
评论 #34456090 未加载
评论 #34458499 未加载
评论 #34456334 未加载
no_wizardover 2 years ago
I hope Records &amp; Tuples[0] land before this does. It would have meaningful and far reaching positive effects for the language, without much controversy. Like most of these things, it takes about 5-7 years for it to permeate through enough of the engines to be meaningfully useful in the day to day of web developers (node &#x2F; deno typically 12-18 months tops). It would drastically speed up existing code once wide adoption is gained though.<p>I don&#x27;t think the Pipe Operator would be as useful in comparison.<p>I really hate how long the Records &amp; Tuple proposal has been languishing at stage 2. It could&#x27;ve shipped <i>years</i> ago if not for a syntax debate that dragged on and on without being fruitful[1]<p>EDIT: there is a class based version of this, that is stage one, for adding structs[2]. They behave similarly.<p>[0]: <a href="https:&#x2F;&#x2F;github.com&#x2F;tc39&#x2F;proposal-record-tuple">https:&#x2F;&#x2F;github.com&#x2F;tc39&#x2F;proposal-record-tuple</a><p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;tc39&#x2F;proposal-record-tuple&#x2F;issues&#x2F;10">https:&#x2F;&#x2F;github.com&#x2F;tc39&#x2F;proposal-record-tuple&#x2F;issues&#x2F;10</a><p>[2]: <a href="https:&#x2F;&#x2F;github.com&#x2F;tc39&#x2F;proposal-structs">https:&#x2F;&#x2F;github.com&#x2F;tc39&#x2F;proposal-structs</a>
评论 #34456883 未加载
评论 #34456639 未加载
评论 #34456687 未加载
dgb23over 2 years ago
Saw a talk with Douglas Crockford[0] years ago. He said something like: Before JS classes got introduced he asked why they didn&#x27;t just implement macros for the language. Classes are in fact just syntactic sugar. Just like async&#x2F;await, and now this proposal.<p>In hindsight he was right. JS would be better off if it did have macros. Much of the whole babel&#x2F;webpack&#x2F;react&#x2F;ts stuff would be just a bunch of macros instead of idiosyncratic build tools and so on. And we would have had much less compatibility churn.<p>In fact this proposal here, is trivial to implement with macros. Clojure has the same thing (threading operator) and it&#x27;s just a macro.<p>[0] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Douglas_Crockford" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Douglas_Crockford</a>
评论 #34456255 未加载
评论 #34458190 未加载
评论 #34460038 未加载
评论 #34456218 未加载
评论 #34456493 未加载
captainmuonover 2 years ago
I don&#x27;t understand why you can&#x27;t just use temporary variables. The article mentions mutation is bad, but what actually happens is that the name gets reassigned. No <i>value</i> is mutated.<p>That brings me to something I really want in JS, actual unmutable values. If you use `const x = new SomeClass()`, you cannot reassign it, but you <i>can</i> change fields. The first time I encountered `const`, I thought it did the opposite. It would be cool if you could declare something (object, array) to be an immutable value.<p>If you really want to introduce new operators, how about operator overloading? For example vector and matrix calculations become a lot clearer and less error-prone with infix operators. It should be technically easy to add them to typescript - rewrite the expression to a function call depending on the types of the operands - but the TS devs refuse to implement this on philosophical grounds unless it is implemented in JS. I guess in JS it would require runtime dispatch, but maybe that is not such a big penalty given that it usually uses a JIT anyway.<p>Oh, and while we are at it, fix `with`. The JS with statement is ridiculous and deprecated anyway. It makes <i>all</i> fields of an object available in it&#x27;s scope. Contrast with VB6&#x27;s `with`, which requires you to use a leading dot and is much more readable:<p><pre><code> with (elem.style) { .fontFamily = &#x27;Arial&#x27;; .color = &#x27;red&#x27;; console.log(.borderWidth); &#x2F;&#x2F; in actual JS this would just be &#x2F;&#x2F; console.log(borderWidth); }</code></pre>
评论 #34455779 未加载
评论 #34457002 未加载
评论 #34461542 未加载
matlinover 2 years ago
Can&#x27;t wait for this. Pipes are awesome in Elixir and bringing them to JS&#x2F;TS will be great.<p>To me this is both concise and readable:<p><pre><code> const weather = `https:&#x2F;&#x2F;api.weather.gov&#x2F;gridpoints&#x2F;TOP&#x2F;31,80&#x2F;forecast` |&gt; await fetch(%) |&gt; await %.json() |&gt; %.properties.periods[0]</code></pre>
评论 #34455424 未加载
评论 #34456578 未加载
评论 #34458530 未加载
评论 #34455058 未加载
评论 #34455077 未加载
评论 #34455345 未加载
snow_macover 2 years ago
I *<i>HATE*</i> pipes. For example from Elixir School (<a href="https:&#x2F;&#x2F;elixirschool.com&#x2F;en&#x2F;lessons&#x2F;basics&#x2F;pipe_operator" rel="nofollow">https:&#x2F;&#x2F;elixirschool.com&#x2F;en&#x2F;lessons&#x2F;basics&#x2F;pipe_operator</a>):<p>``` foo(bar(baz(new_function(other_function())))) ```<p>They offer this example of an improvement:<p>``` other_function() |&gt; new_function() |&gt; baz() |&gt; bar() |&gt; foo() ```<p>While yes, pipes improve readability, how do they deal with errors? How do they deal with understanding what each thing is supposed to return?<p>I would prefer something like this, (descriptive variable names):<p>``` var userData = other_function();<p>var userDetails = new_function(userData);<p>var userComments = baz(userDetails);<p>var userPosts = bar(userComments);<p>var finalUserDetails = foo(userPosts);<p>return finalUserDetails; ```<p>Then I can easily debug each step, I can easily understand what each call is supposed to do, if I&#x27;m using type script, I can assign types to each variable.<p>I strongly oppose clean code for the sake of looking pretty, or being quick to type. Code is meant to be run and read more then written, it should be descriptive, it should describe what it&#x27;s doing not a nasty chain of gibberish. Hence why most people hate REGEX.
评论 #34457656 未加载
评论 #34458890 未加载
vorotatoover 2 years ago
These hack pipes are a trojan horse. People wanted elixir&#x2F;F#&#x2F;ocaml aka function pipes, and what we got was unreadable line noise. I argued against it until I was blue in the face, decided it was bad for my general wellness to keep it up. I genuinely would prefer no pipes over this. I couldn&#x27;t find a single example where I preferred it. The token they chose already has a meaning in Javascript! The arrogance and willful disregard for readable code was astonishing. The only tangible reason I could pull as to why they picked the least popular implementation despite all the outrage was &quot;someone at google didn&#x27;t like the function pipes&quot;. Even if you think we should avoid it because some google employee doesn&#x27;t want it, that doesn&#x27;t mean you should ram in an even worse implementation. I had to block the TC39 discussion because I was just going to get argumentative because they weren&#x27;t listening at all, and they were dismissing actual concerns without any explanation.
评论 #34470755 未加载
jsf01over 2 years ago
This strikes me as something better left to libraries. If you want to write in a functional style then Ramda, Lodash, Underscore, and plenty of others have pipe and compose functions.<p><pre><code> pipe(one, two, three) </code></pre> Easy to read. No new syntax. Extendable with arrow functions.<p>Yes, there are some limitations in comparison to Hack Pipes. But those are far outweighed by not messing yet again with the language’s syntax.
评论 #34456085 未加载
评论 #34455657 未加载
评论 #34462682 未加载
Someone1234over 2 years ago
Can someone remind me again why there&#x27;s never been movement to add a second modern language to web-browsers? JavaScript was created in a weekend and then stuff tacked on for the last 28 years.<p>We know so much more about how to create programming languages today than we did then, and the whole &quot;Year of the Linux Desktop&quot; has become a WebAssembly meme now every year since its introduction six years ago, with it getting popular always next year, next version, with feature XYZ. Seemingly creating unmaintainable&#x2F;debuggable mess from external languages with no true 1:1 into WebAssembly isn&#x27;t as big of a hit as the originators expected.<p>Yet every time someone asks why there hasn&#x27;t been movement here it is &quot;Year of WebAssembly is next year!!!&quot; WebAssembly has managed to slow actual progression towards something good. With the browser monoculture you&#x27;d think it would be easier now than ever to start a fully integrated second language with WebAssembly compilation for backwards compatibility.
评论 #34455435 未加载
评论 #34455566 未加载
评论 #34455417 未加载
评论 #34455489 未加载
评论 #34455754 未加载
评论 #34460496 未加载
评论 #34456117 未加载
zelphirkaltover 2 years ago
For languages, which do not have built-in the power to change themselves, in many cases it might be better to stick to their feature set, instead of introducing even more language concepts. Look at how much work is involved to get something as simple as pipelines. As if they will ever find the right syntax for everyone.<p>If we used a normal function we might have to include a library or a dependency or heck, just take 5 minutes and write a pipeline function oneself. Sure, it will not be syntactically as minimal as a _change of the language itself_ to allow pipelining, but at least it will not introduce even more language features and everyone can easily look at the definition change it or include it in their own projects.<p>Ideally we would strive for a minimalistic set of features, which in turn can implement anything we want. Adding more and more features, without introducing a way for the user to modify the language without a commitee and a lengthy process, seems short-sighted.<p>If you want to give the user more power over syntax, introduce a well engineered macro mechanism (plenty of examples in other languages) and let people develop standards and libraries as macros and libraries. Later on decide what to take into the standard language. Similar to how jQuery influenced modern JS standard functions like querySelectorAll. Even if you don&#x27;t take something into the standard language, users are still free to include it in their project. No harm done and everyone gets their lunch and can scratch their itches.
评论 #34458768 未加载
eknkcover 2 years ago
I&#x27;m pretty sure this&#x27;d be a welcome addition to the language but to be honest, the syntax looks shit.
评论 #34454557 未加载
ravenstineover 2 years ago
As someone who actually loves JavaScript, I think this is a really bad idea.<p>Maybe it has a place in other languages. I really don&#x27;t want to see it in JS. We don&#x27;t need more ways to do things implicitly or bass-ackwards from how they&#x27;re actually behaving underneath. Syntactic sugar rarely makes code more readable. This operator only makes code seem more concise and as if it&#x27;s executing in a way that it actually isn&#x27;t.<p>I can absolutely see junior developers running wild with this kind of thing.<p>JS should be kept <i>simple</i>. This operator is not simple. It now makes understanding expressions more complicated. JS has its quirks and rough edges, but its power is in its simplicity. Please do not import the mistakes of other languages into JavaScript. If someone wants this operator, they should be forced to use a Babel transform or to compile their own JS interpreter.<p>OR just compile your favorite language runtime with the pipe operator to WASM.
__ryan__over 2 years ago
An alternative is to make the pipe operator a simple function application and provide syntax for creating simple pipeline functions.<p>For example:<p><pre><code> left |&gt; right </code></pre> Would semantically translate to:<p><pre><code> right(left) </code></pre> And you could define a pipeline function like so, where the following:<p><pre><code> const myPipeline = @[ one(@), @.two(), @ + three, `${@} four` ] </code></pre> Would translate to:<p><pre><code> const myPipeline = (value) =&gt; { const _1 = one(value); const _2 = _1.two(); const _3 = _2 + three; const _4 = `${_3} four`; return _4 } </code></pre> Or:<p><pre><code> const myPipeline = (value) =&gt; `${one(value).two() + three} four`; </code></pre> And you could define the placeholder value name (which would allow nesting):<p><pre><code> const myPipeline = @it [ one(@it), @it.two(), @it + three, `${@it} four`, ] </code></pre> You&#x27;d combine the two syntaxes to get immediately-invoked pipeline functions:<p><pre><code> &#x2F;&#x2F; Using a modified example from the proposal: envars |&gt; @ [ Object.keys(@), @.map(envar =&gt; `${envar}=${envars[envar]}`), @.join(&#x27; &#x27;), `$ ${@}`, chalk.dim(@, &#x27;node&#x27;, args.join(&#x27; &#x27;)), console.log(@), ] </code></pre> This is better, in my opinion, than building the &#x27;%&#x27; placeholder syntax into the pipe operator.
AirMax98over 2 years ago
That % syntax is just completely unlike anything else I have seen in JS.<p>As a multi paradigm language, JS typically suffers from whatever programming style is on trend when these features are implemented. We are apparently on the other side of the pendulum now, but I can’t remember the last time I worked with a class and felt like that was right either.
评论 #34457119 未加载
评论 #34465875 未加载
asciimovover 2 years ago
This just complicates things if you have any kind of complex nesting.<p>Something &quot;simple&quot; like:<p><pre><code> a(b(),c(),d(e(),f(g()))) </code></pre> Turns into the following:<p><pre><code> value |&gt; b() |&gt; a( %, c() , v2 |&gt; e() |&gt; d(%, v1 |&gt; g() |&gt; f(%) ))</code></pre>
评论 #34455713 未加载
评论 #34455660 未加载
zackmorrisover 2 years ago
Cool stuff, but I miss the &quot;it&quot; variable from HyperTalk (the language used by HyperCard) which contained the result of prompts to the user. Just search for &quot;The it variable&quot;:<p><a href="http:&#x2F;&#x2F;www.jaedworks.com&#x2F;hypercard&#x2F;HT-Masters&#x2F;scripting.html" rel="nofollow">http:&#x2F;&#x2F;www.jaedworks.com&#x2F;hypercard&#x2F;HT-Masters&#x2F;scripting.html</a><p><pre><code> ask &quot;How many minutes do you want to play?&quot; put it * 60 into timeToPlay -- convert it into seconds </code></pre> Today we could have a reserved keyword that holds the result of the last statement executed. A practical example adapted from the article using &quot;it&quot; might look like:<p><pre><code> Object.keys(envars) it.map(envar =&gt; `${envar}=${envars[envar]}`) it.join(&#x27; &#x27;) `$ ${it}` chalk.dim(it, &#x27;node&#x27;, args.join(&#x27; &#x27;)) console.log(it); </code></pre> A better name for &quot;it&quot; today might be &quot;_&quot;, &quot;result&quot; or perhaps &#x27;$&#x27; in a shell-inspired language like php. Most shells support &quot;$?&quot; so that could work too:<p><pre><code> # prints 0 true ; echo $? # prints 1 false ; echo $?</code></pre>
评论 #34457354 未加载
评论 #34576223 未加载
mgover 2 years ago
I wonder if &quot;%&quot; placeholder is the right approach. It makes the code longer in most cases.<p>Without pipes:<p><pre><code> a = d(c(b)) </code></pre> With pipes in the proposed form:<p><pre><code> a = b|&gt;c(%)|&gt;d(%) </code></pre> My first approach to design it would be:<p><pre><code> a = b~&gt;c~&gt;d </code></pre> So the rule is that on the right side of the pipe operator (~&gt;) there is always a function. We don&#x27;t need parenthesis to indicate that.<p>If the function takes more than one argument, it can be defined by another value on the left of it.<p>Without pipes:<p><pre><code> a = d(c(b,7)) </code></pre> With pipes in the proposed form:<p><pre><code> a = b|&gt;c(%,7)|&gt;d(%) </code></pre> With the ~&gt; approach:<p><pre><code> a = b,7~&gt;c~&gt;d</code></pre>
评论 #34458609 未加载
charles_fover 2 years ago
&gt; three(two(one(value)))<p>const oned = one(value);<p>const twoed = two(oned);<p>const threed = three(twoed);<p>This proposition goes out of its way to find problems with code that is written in a confusing and uncommon way in the first place.
评论 #34454804 未加载
jibberjabb3rover 2 years ago
The proposed pipe operator could only be efficiently implemented via a transpiler pass to lower it to &quot;regular&quot; JS varaibles. I wouldn&#x27;t want this feature in a JS engine due to the overhead it would require. Sometimes it&#x27;s better just to say no to new features that yield questionable utility and don&#x27;t reduce code size or complexity by much.
vlunkrover 2 years ago
I love the pipe operator in Elixir, but I&#x27;ve never really wanted it in JS. It&#x27;s critical in Elixir because the design of the runtime (immutability, functions only, no methods). In the rare cases that you might need it, like their three(two(one(value))) example, function composition is available from libraries or easy to do yourself.<p>It&#x27;s just my opinion, but I think turning JS into a kitchen sink of language features is a mistake.
mihaicover 2 years ago
I know I&#x27;m in the minority, but I dislike both version of the syntax, since I think chained code is almost always worse than just being forced to use intermediary variables for everything.<p>While it&#x27;s fine to say Object.entries().map(), anything more is not only harder to read, but makes debugging and maintenance more difficult.
jansommerover 2 years ago
Certainly looking forward to the pipe operator - if it ever lands! It doesn&#x27;t seem like it&#x27;s moved to the next stage recently in the commit history, and it&#x27;s been discussed for ages now.
pharmakomover 2 years ago
The pipe operator is awesome because you can use it to “extend” objects without messing with their prototypes.<p>Missing String.titleCase ? Write your own!<p><pre><code> “hello world” |&gt; titleCase</code></pre>
评论 #34455011 未加载
评论 #34455126 未加载
评论 #34454840 未加载
gfodorover 2 years ago
Pipes are cool but this falls pretty hard on the side of adding too much cruft to the language.
dahartover 2 years ago
&gt; Deep nesting is hard to read […] Temporary variables are often tedious<p>I’m a little torn because pipes might be pretty nice, especially for prototype code and small projects. One thing this proposal doesn’t acknowledge is that for production code, deep nesting’s often impractical and often considered an anti-pattern in the first place, so making it easier isn’t a common need or problem to have in my experience. Usually I’m going the other way, having to make it more tedious. Using temporary variables and breaking apart nesting is, far more often than not, necessary in order to do proper error checking, and just to make code readable, commentable, and refactorable, etc. I feel like what we need is not a way to make deep nesting easier to read, it’s a way to make temporary variables less tedious, perhaps while also piping from one function to the next… <i>that</i> would be really helpful in a deeper way than just adding another chaining syntax. Is the syntax is stuck at “|&gt;”? I guess it’s not possible to override bitwise-or (“|”), but “|&gt;” feels maybe a little clunky?
de_keyboardover 2 years ago
At work I am always hopping between F# and JavaScript and I&#x27;m <i>convinced</i> that if more people had this experience they would want the pipe operator in JavaScript.<p>Unfortunately it&#x27;s hard to convey the advantages to those who haven&#x27;t used it. Maybe it&#x27;s because it&#x27;s such a simple bit of syntatic-sugar?
leishmanover 2 years ago
I love the pipe operator in Elixir!
评论 #34455455 未加载
评论 #34454453 未加载
评论 #34455343 未加载
评论 #34456886 未加载
evnpover 2 years ago
const isRandNumOdd = Math.round(Math.random() * 100) |&gt; % % 2 |&gt; Boolean;<p>Excruciatingly contrived, but does this sort of arithmetic work in the Hack syntax? I&#x27;m genuinely curious, couldn&#x27;t find any mention of modulo (or remainder) in the proposal.
评论 #34455179 未加载
nassimsoftwareover 2 years ago
To give credit where it&#x27;s due. I came across the proposal through this video : <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=h1FvtIJ6ecE">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=h1FvtIJ6ecE</a> by Theo the CEO of Ping.gg.
评论 #34454961 未加载
bborudover 2 years ago
Yeah, sure, just heap in more stuff. Because it isn&#x27;t chaotic and ugly enough yet.
MathMonkeyManover 2 years ago
Can&#x27;t javascript already do [this][1]?<p><pre><code> const thrush = (value, func, ...funcs) =&gt; func ? thrush(func(value), ...funcs) : value; &#x2F;&#x2F; no new syntax required thrush( envars, Object.entries, x =&gt; x.map(([key, val]) =&gt; `${key}=${val}`), x =&gt; x.join(&#x27; &#x27;), x =&gt; chalk.dim(&#x27;$ &#x27; + x, &#x27;node&#x27;, args.join(&#x27; &#x27;)), console.log); </code></pre> [1]: <a href="http:&#x2F;&#x2F;www.davidgoffredo.com&#x2F;thrush.html" rel="nofollow">http:&#x2F;&#x2F;www.davidgoffredo.com&#x2F;thrush.html</a>
评论 #34463977 未加载
pixel_tracingover 2 years ago
Well here’s a solution to a problem that didn’t really need any solving.<p>How about spending time on:<p>• Actors • Real Immutable Structs<p>?<p>This is going to create more code unreadable, while it may be cool and clever to put a pipe in and call it a day, I imagine most production code will be a pile of nested pipe gibberish that any junior engineer or new hire would pull their hair at trying to piece together.<p>We want to create features, and solve business problems and that in turn also requires maintenance. This is just some clever macro wrapper around currying. While functional style is cool and all I don’t really see much value for most day to day workflows
noivover 2 years ago
I had a hard time rethinking decades of frontend projects where pipes would simplify code at least two times. However, in the backend please go ahead.<p>Having said that, can anybody provide an example with error handling per pipe?<p>You know servers are bitches :)
masswerkover 2 years ago
I don&#x27;t think that it&#x27;s a good idea to introduce a left-to-right flow into a language, which strictly assigns righthand values by general design. The text mentions a back-and-forth in reading, this introduces a back-and-forth intellectually.<p>(There&#x27;s already a limited left to right capability, namely by chaining expressions using logical operators, especially when used outside of condition. It should be mentioned, however, that this is already confusing to some.)
评论 #34459881 未加载
pier25over 2 years ago
This is cool but it always frustrates me to see these the TC39 focusing on these little improvements to the language instead of taking big bold steps that would have a much more significant impact.<p>Stuff like types, data binding, reactivity, etc. These would save so many kbs and CPU cycles if implemented natively. The world sorely needs that. God knows how much energy is wasted in sending and processing huge bundles of JS billions of times every day.
dmakover 2 years ago
This is similar to Elixir. It&#x27;s such a natural way to program and so useful. I wish all languages had this.<p>Also shout out to bash pipes
jrockwayover 2 years ago
&gt; In the State of JS 2020 survey, the fourth top answer to “What do you feel is currently missing from JavaScript?” was a pipe operator.<p>In 2021 (<a href="https:&#x2F;&#x2F;2021.stateofjs.com&#x2F;en-US&#x2F;opinions&#x2F;" rel="nofollow">https:&#x2F;&#x2F;2021.stateofjs.com&#x2F;en-US&#x2F;opinions&#x2F;</a>) it fell to 7th place.
tambourine_manover 2 years ago
JavaScript continues on its eternal and unavoidable march from a small quirky language towards a huge quirky language.
itslennysfaultover 2 years ago
Maybe it&#x27;s just that I&#x27;m just old and been doing JavaScript for 20+ years, but I HATE IT.
trekkie1024over 2 years ago
I actually find the original easier to read than the piped example given for this one:<p>Original<p><pre><code> jQuery.merge( this, jQuery.parseHTML( match[ 1 ], context &amp;&amp; context.nodeType ? context.ownerDocument || context : document, true ) ); </code></pre> -----------------------------------------<p>With pipes<p><pre><code> context |&gt; (% &amp;&amp; %.nodeType ? %.ownerDocument || % : document) |&gt; jQuery.parseHTML(match[1], %, true) |&gt; jQuery.merge(%); </code></pre> I think F# pipes are ideal in more complex cases, the % can add unnecessary complexity when reading code. Alas, it looks like we&#x27;re not going to get that.
agumonkeyover 2 years ago
I played with coconut recently (fp layer on top of python) and the `|&gt;` syntax felt very annoying compared to `.` (even though it felt ok in Ocaml..)<p>But in an OO underlying language it&#x27;s probably impossible to reuse it.
shadowgovtover 2 years ago
This makes me nervous.<p>In general, I think adding features like this to a mature language is a misstep because it increases the cognitive load of &quot;things you have to know to read other people&#x27;s code.&quot; And that&#x27;s <i>strictly</i> increases... Since changes like this can&#x27;t remove previous approaches (for backwards compatibility reasons), we&#x27;ll now have <i>three</i> syntaxes for function calls? Yuck.<p>Left unchecked, this predilection eventually leaves you with languages like C++: a language where you <i>can</i> write good, safe code if you stick to modern methods, but <i>good luck</i> learning what &quot;modern methods&quot; are or finding tutorial books that don&#x27;t teach you any of the bad-old approaches or, most importantly, working with other people&#x27;s C++ code that still has `setjjmp` and `longjmp` in it because the language allows for it, therefore <i>someone used it somewhere.</i><p>(oblig: <a href="https:&#x2F;&#x2F;xkcd.com&#x2F;927&#x2F;" rel="nofollow">https:&#x2F;&#x2F;xkcd.com&#x2F;927&#x2F;</a>)
评论 #34456079 未加载
01acheruover 2 years ago
The syntax is simply disgusting, some examples look worse with pipes than without them.<p>I understand it is stage 2 now but can we do something about it? I&#x27;m seeing that most comments here and in other places are critical, why should we get this stuff in JS because of a vocal minority that is trying to push it on everyone else?<p>If we keep adding the fifth thing asked on state of JS year after year we will end with some frankenstein weirdness... JS was getting better and better, let&#x27;s not make it worse.
brundolfover 2 years ago
This proposal has been languishing for years and years. I&#x27;m not sure when it last made progress, but I remember waiting for it with anticipation around 2018
8K832d7tNmiQover 2 years ago
Yeah, no. The idea of even reversing function order just to use the piping operator itself is already bad enough to even consider using it.
Name_Chawpsover 2 years ago
Constant remodeling is the sign of an ugly home.
0xmarcinover 2 years ago
Seems to me that someone &quot;stole&quot; it from F#. F# probably also take this idea from some unknown (to me) language. Here a similar idea in C++ <a href="https:&#x2F;&#x2F;www.fluentcpp.com&#x2F;2019&#x2F;10&#x2F;18&#x2F;piping-to-and-from-a-stream&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.fluentcpp.com&#x2F;2019&#x2F;10&#x2F;18&#x2F;piping-to-and-from-a-st...</a><p>This is generally a weakness of text based programming languages, that you cannot easily express graph flows like:<p><pre><code> &#x2F; B1 \ A&gt;-| | -&gt; C \ B2 &#x2F; </code></pre> |&gt; operator only solves the problem for non-branched flows like A -&gt; B -&gt; C making them more readable by removing nested calls.<p>In theory you can create something similar using JS OOP by attaching e.g. map&#x2F;use(func) methods to every prototype:<p><pre><code> function use(func) { func(this); return this; } function map(func) { return func(this); } </code></pre> and then:<p><pre><code> (1).map(n =&gt; n+1) .use(console.log) .map(n =&gt; &quot;n = &quot; + n) .use(n =&gt; console.log(&quot;str = $n&quot;)); </code></pre> Introducing a new operator instead of a library is a huge effort. I don&#x27;t think this proposal will succeed, especially that current custom operator support in JS is nil.
6510over 2 years ago
<p><pre><code> three(two(one(value)))</code></pre> .<p><pre><code> value |&gt; one() |&gt; two() |&gt; three()</code></pre> .<p><pre><code> a=one(value); a=two(a); three(a);</code></pre> .<p><pre><code> pipe(value, one, two, three) </code></pre> all seem fine but nr 2, where does the return value from three() end up?
CyberDildonicsover 2 years ago
I don&#x27;t see how syntactic sugar makes any sense for javascript. Breaking compatibility is so severe because every browser needs to catch up, yet it doesn&#x27;t actually enable anything that couldn&#x27;t be done before.
评论 #34454577 未加载
评论 #34454456 未加载
评论 #34454503 未加载
评论 #34454598 未加载
评论 #34454556 未加载
joshxyzover 2 years ago
This feels like code golfing. To messy to explain to newbies. Just extra friction.<p>You have to explain how the syntax works, the environment it works in, and the error handling. What the hell.
born-jreover 2 years ago
LETS FREEzE JS, why are we adding more stuff to js ? (੭ ˊ^ˋ)੭
adamrezichover 2 years ago
previously: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=28916775" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=28916775</a>
thot_experimentover 2 years ago
This language is a garbage pile, a literal scrap heap. Look over there, that&#x27;s the kitchen sink. For fucks sake we added the absolute aesthetic disaster that is CLASSES to the thing. The strength of this language is that we throw in everything and the kitchen sink. I&#x27;ve had one wish for the last 5 years and it&#x27;s for this god damn operator to land in the language. Please just throw it in there like everything else, it will make my life so much more pleasant. If you don&#x27;t like it you can just ignore it studiously just like I do with classes.
ht85over 2 years ago
I&#x27;ll only allow it if you can pipe `|&gt; debugger`
jonnycomputerover 2 years ago
Anyone doing data analysis knows the value of a pipe.
grumpyproleover 2 years ago
The &quot;Hack&quot; pipe really is a bit of a hack. It conflates two orthogonal issues, a consise lambda syntax and function application.
heliodorover 2 years ago
What a pain to type! The idea is great though.
erixMover 2 years ago
What if you have to use the remainder operator? How would &quot;|&gt; % % 2&quot; work?
评论 #34472664 未加载
e-dantover 2 years ago
Functions are universal. Choose them over everything else.<p>Functional F# syntax or bust.
nmnvzrover 2 years ago
It looks super ugly.
icholyover 2 years ago
Please god no more...
hajileover 2 years ago
They should have stuck with the F# proposal. The hack proposal just takes one more giant step toward turning JS into Perl.<p>Hack proposal<p><pre><code> value |&gt; foo(%) for unary function calls, value |&gt; foo(1, %) for n-ary function calls, value |&gt; %.foo() for method calls, value |&gt; % + 1 for arithmetic, value |&gt; [%, 0] for array literals, value |&gt; {foo: %} for object literals, value |&gt; `${%}` for template literals, value |&gt; new Foo(%) for constructing objects, value |&gt; await % for awaiting promises, value |&gt; (yield %) for yielding generator values, value |&gt; import(%) for calling function-like keywords, </code></pre> F# proposal<p><pre><code> value |&gt; x=&gt; x.foo() for method calls, value |&gt; x=&gt; x + 1 for arithmetic, value |&gt; x=&gt; [x, 0] for array literals, value |&gt; x=&gt; ({foo: x}) for object literals, value |&gt; x=&gt; `${x}` for template literals, value |&gt; x=&gt; new Foo(x) for constructing objects, value |&gt; x=&gt; import(x) for calling function-like keywords, </code></pre> F# proposal would make `await` and `yield` into special syntax cases or not allowed.<p>I&#x27;d rather do await&#x2F;yield the old fashioned way (or slightly complicate the already complex JS syntax rules) than add the weird extra syntax. Arrow functions are elegant and already well-known and well-understood.
评论 #34454855 未加载
评论 #34454846 未加载
评论 #34454815 未加载
评论 #34454909 未加载
评论 #34454732 未加载
评论 #34454511 未加载
评论 #34454649 未加载
评论 #34457235 未加载
评论 #34455917 未加载
评论 #34454911 未加载
chadlaviover 2 years ago
JavaScript isn&#x27;t that kind of programming language. I don&#x27;t know why people are constantly trying to make it something else.
评论 #34454768 未加载
评论 #34454648 未加载
评论 #34454864 未加载
评论 #34454824 未加载
评论 #34473357 未加载
评论 #34455428 未加载
评论 #34455278 未加载
评论 #34454686 未加载