TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

F# Pipeline Operator in JavaScript with Babel

114 点作者 galacticdessert超过 5 年前

24 条评论

zackmorris超过 5 年前
Somewhat related, but I wish that more languages provided a reserved convenience variable for accessing the result of the previous function call:<p><a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;3326826&#x2F;language-history-origin-of-variable-it-in-read-eval-print-loop&#x2F;3342195" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;3326826&#x2F;language-history...</a><p><pre><code> Python and Ruby: _ Shell: $? HyperTalk: it (my first exposure to the concept, even though it was more of a convention) Some Lisps: *, **, ***, ... </code></pre> If we had that, then we could write:<p><pre><code> func1(); func2(_); func3(_); ... </code></pre> Which reminds me of PostScript, which lets you examine and manipulate the program&#x27;s stack directly (the top of the stack is the previous result):<p><a href="http:&#x2F;&#x2F;www.ugrad.math.ubc.ca&#x2F;Flat&#x2F;intro.html" rel="nofollow">http:&#x2F;&#x2F;www.ugrad.math.ubc.ca&#x2F;Flat&#x2F;intro.html</a><p><a href="http:&#x2F;&#x2F;www.ugrad.math.ubc.ca&#x2F;Flat&#x2F;stack.html" rel="nofollow">http:&#x2F;&#x2F;www.ugrad.math.ubc.ca&#x2F;Flat&#x2F;stack.html</a><p>If I were implementing it, I would make the previous result and stack contents read-only so that it&#x27;s conceptually the same as the pipeline operator except that it allows inspecting the intermediate value.
评论 #22237186 未加载
评论 #22244676 未加载
评论 #22236888 未加载
alharith超过 5 年前
There&#x27;s also ReasonML for those looking to scratch the itch sooner.<p>Also, don&#x27;t be turned off by the sea of comments. Pick one, they are all great. but yes ML language enthusiasts can be a bit overenthusiastic, but I don&#x27;t fault them for that. They are all anxious to spread the gospel of ML. The Reason community on discord is excellent, one of the best I have had the pleasure to be a part of.<p>I didn&#x27;t go with straight OCaml with js_of_ocaml because I wanted a better representation of what I was doing in JS land. There is an excellent write-up about it here: <a href="https:&#x2F;&#x2F;www.javierchavarri.com&#x2F;js_of_ocaml-and-bucklescript&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.javierchavarri.com&#x2F;js_of_ocaml-and-bucklescript&#x2F;</a>
评论 #22236350 未加载
评论 #22235989 未加载
评论 #22236894 未加载
TheAsprngHacker超过 5 年前
Why do people credit the pipe operator to F#? OCaml has it too, and F# is based on OCaml. Did the pipe operator get added to F# first? (The OCaml docs say that the pipe operator first appeared in OCaml 4.01 [1], and I can&#x27;t find out when it got added to F#.)<p>[1] <a href="https:&#x2F;&#x2F;caml.inria.fr&#x2F;pub&#x2F;docs&#x2F;manual-ocaml&#x2F;libref&#x2F;Stdlib.html" rel="nofollow">https:&#x2F;&#x2F;caml.inria.fr&#x2F;pub&#x2F;docs&#x2F;manual-ocaml&#x2F;libref&#x2F;Stdlib.ht...</a>
评论 #22241536 未加载
评论 #22241506 未加载
评论 #22242770 未加载
评论 #22241369 未加载
评论 #22241066 未加载
tombert超过 5 年前
It&#x27;s times like these that make me realize how spoiled I am by ClojureScript...the -&gt; and -&gt;&gt; macros basically give me a pipe operator, and have been in the language from its inception :)<p>I&#x27;m actually glad that this kind of composition is growing in popularity. Back when I did F# for a living, I loved that by using the pipe operator, you could get something more or less akin to a fluent interface, without any direct coupling between the two composed functions, and without any gross intermediate variables. I have nothing against &quot;regular&quot; point-free composition or anything like that, but I do think that these pipe operators are easier to digest for a lot of purposes.
评论 #22244736 未加载
adgasf超过 5 年前
I find JavaScript unusable without this operator. Fortunately Babel supports it well.<p>It makes a great alternative to fiddling with prototypes or wrapper objects when you want to extend something.<p>For example, this is flat-map implemented as a free function:<p><pre><code> const flatMap = f =&gt; { if (!f) { throw new TypeError(&#x27;f must be a function&#x27;); } return xs =&gt; ({ [Symbol.iterator]: function * () { for (const x of xs) { yield * f(x); } } }); }; &#x2F;&#x2F; Usage const xs = [ 1, 2, 3 ] |&gt; flatMap(x =&gt; [ x, -x ]);</code></pre>
lf-non超过 5 年前
For people who want something similar that is also typescript friendly, I wrote a babel macro that entirely gets compiled away during build.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;ts-delight&#x2F;pipe.macro" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ts-delight&#x2F;pipe.macro</a>
jiofih超过 5 年前
That fetch &#x2F; await example is horrendous. Please don’t.
lwb超过 5 年前
Anyone else think the async&#x2F;await example looks terrible? Why do the keywords have to be on their own line?
egeozcan超过 5 年前
I had created a solution to scratch my itch while waiting for the pipe operator which even got a logo when it got more than a hundred stars, for anyone interested: <a href="https:&#x2F;&#x2F;github.com&#x2F;egeozcan&#x2F;ppipe" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;egeozcan&#x2F;ppipe</a> (needs ts typings though)
评论 #22236558 未加载
koboll超过 5 年前
This is nice syntax, but since there are two competing proposals and they&#x27;re collectively at stage 1, this isn&#x27;t something anyone should really be using for nontrivial code, since there&#x27;s a good chance either way that you&#x27;ll be writing JavaScript that will never be valid.
IggleSniggle超过 5 年前
I know this proposal has been sitting around forever, but it really can&#x27;t come fast enough imho. I don&#x27;t know how to help show my support for its inclusion, however. Can anyone help elucidate?
评论 #22236910 未加载
评论 #22235948 未加载
评论 #22238427 未加载
评论 #22242573 未加载
评论 #22235979 未加载
评论 #22235858 未加载
vmchale超过 5 年前
F# pipeline operator aka... function composition that has existed in other MLs forever?
评论 #22237011 未加载
评论 #22236704 未加载
评论 #22236684 未加载
评论 #22237915 未加载
waylandsmithers超过 5 年前
I enjoy the utility of this operator in Elixir but I&#x27;m not sure how I feel about including it in JS.<p>My fear is that JS is trying to be all things to all developers- it feels like we just added the `class` keyword for the OO inclined and now we&#x27;re moving towards functional programming? Do we really want to move further away from the idea of idiomatic JS?<p>But who knows, maybe it will open the door to massive productivity and enjoyability gains with the language...
评论 #22241769 未加载
评论 #22241480 未加载
blunte超过 5 年前
What was the author thinking with that pointless meme image?
评论 #22244159 未加载
评论 #22241609 未加载
kummappp超过 5 年前
if you add multiplication functionality, identity and termination symbols to it, you get something that is closer to a proper category like done in this: <a href="https:&#x2F;&#x2F;github.com&#x2F;kummahiih&#x2F;python-category-equations" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kummahiih&#x2F;python-category-equations</a><p>f1(?) |&gt; ( f2(?), I ) |&gt; f3(?) == f1(?) |&gt; f2(?) |&gt; f3(?) , f1(?) |&gt; f3(?)<p>it just feels natural that way
WouterSpaak超过 5 年前
Fun fact: the `pipe` function from `rxjs` can be imported on its own where it will work on any value of type T, not just `Observable&lt;T&gt;`.
thibran超过 5 年前
I wrote something similar for Scheme - <a href="https:&#x2F;&#x2F;cons.io&#x2F;reference&#x2F;sugar.html#chain" rel="nofollow">https:&#x2F;&#x2F;cons.io&#x2F;reference&#x2F;sugar.html#chain</a>. It&#x27;s interesting to see that other people came up with a quite similar solution, providing two modes: pass-directly, pass-by-variable. Does the JS version supports destructuring?
评论 #22240982 未加载
reaktivo超过 5 年前
I think it&#x27;s worth nothing that a full Promise handling pipe function can be written like so:<p><pre><code> const pipe = (...args) =&gt; args.reduce(async (acc, fn) =&gt; fn(await acc)); </code></pre> No actual third party libraries required
platz超过 5 年前
It&#x27;s nice that you can&#x27;t typo your variables when writing point-free code
chaorace超过 5 年前
For those of you who work with Node, highland.js is another great route for composable programming. It has excellent support for Node streams, which has made it quite handy with Gulp especially!
bjoli超过 5 年前
The 5 codebox does filter map reduce. That makes me wonder, is there any loop fusion going on in JS? Or are there any good libraries for transducers?
seanwilson超过 5 年前
What happens when you need to debug and see how the data looks at each stage of the transformation pipeline? Won&#x27;t the debugger show this as a single step if you use the pipeline operator? It does look cleaner and means you don&#x27;t have to spend time coming up with names for the intermediate results though.
评论 #22239191 未加载
评论 #22237852 未加载
sbussard超过 5 年前
Why not use the haskell bind operator? They&#x27;re similar, right?
评论 #22238323 未加载
评论 #22241231 未加载
评论 #22238171 未加载
dimgl超过 5 年前
Thanks, I hate it.