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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

R adds native pipe and lambda syntax

250 点作者 _fnhr超过 4 年前

17 条评论

wenc超过 4 年前
I&#x27;m sure some of us who are out of the loop might be wondering: what about the magrittr pipe operator (%&gt;%) that we all know and love?<p>Luke Tierney explains the move from %&gt;% to a native pipe |&gt; here [1]. The native pipe aims to be more efficient as well as addresses issues with the magrittr pipe like complex stack traces.<p>Turns out the |&gt; syntax is also used in Julia, Javascript and F#.<p>The lambda syntax (\(x) -&gt; x + 1) is similar to Haskell&#x27;s (\x -&gt; x + 1).<p>[1] <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=X_eDHNVceCU&amp;feature=youtu.be&amp;t=3099" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=X_eDHNVceCU&amp;feature=youtu.be...</a>
评论 #25318658 未加载
评论 #25318318 未加载
评论 #25317195 未加载
评论 #25317273 未加载
评论 #25319548 未加载
评论 #25317579 未加载
评论 #25317197 未加载
评论 #25320422 未加载
评论 #25317389 未加载
cwyers超过 4 年前
The R &quot;userland&quot; pipe magrittr has been altered to make it more compatible with the proposed base pipe, as well:<p><a href="https:&#x2F;&#x2F;www.tidyverse.org&#x2F;blog&#x2F;2020&#x2F;11&#x2F;magrittr-2-0-is-here&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.tidyverse.org&#x2F;blog&#x2F;2020&#x2F;11&#x2F;magrittr-2-0-is-here&#x2F;</a>
burlesona超过 4 年前
That backslash syntax is pretty funky, but then again all of R is a little funky. Very nice addition to the language though!
评论 #25319455 未加载
评论 #25318256 未加载
评论 #25317033 未加载
bluenose69超过 4 年前
These will both come in pretty handy, although the first is just a formalization of something that&#x27;s already available in packages, the lambda syntax will clean up code a fair bit, and make realtime analysis easier to type.
saeranv超过 4 年前
Question by someone who is ignorant but interested in functional programming: what is the closest equivalent to these functions in Python? (Or correct me if I&#x27;m asking the wrong question).<p>I used to love using lambdas in Python, along with map&#x2F;reduce&#x2F;filter but for whatever reason the Python community has turned against it. Map and filter can now be nicely done with list comprehensions, although I still haven&#x27;t found a decent one-line equivalent for reduce (other then importing functools).
评论 #25320516 未加载
评论 #25319387 未加载
评论 #25320689 未加载
评论 #25331696 未加载
identity0超过 4 年前
I wish more languages gave us a &quot;|&gt;&quot; operator. Too many languages settle with dot notation, which confuses encapsulation&#x2F;method calling with syntactical convenience.
评论 #25319320 未加载
wodenokoto超过 4 年前
Kind of odd they didn&#x27;t decide to go with the magrittr syntax, which is in common use and heavily promoted in dplyr &#x2F; tidyverse.<p>I wonder if RStudio will change its `ctrl` + `shift` + m shortcut from the magrittr ( %&gt;% ) style pipes to these new pipes ( |&gt; )
评论 #25317653 未加载
评论 #25317266 未加载
评论 #25317643 未加载
评论 #25319304 未加载
评论 #25317353 未加载
civilized超过 4 年前
This sounds promising, but how do we type that pipe easily if we&#x27;re going to be using it all the time? I actually like %&gt;% because it&#x27;s easy to reach the keys and hammer it out. Agreed on the ugly stack traces.
评论 #25317847 未加载
评论 #25317837 未加载
tpoacher超过 4 年前
For people asking how one could possibly ever manage to chain operations in python without a pipe operator: <a href="https:&#x2F;&#x2F;github.com&#x2F;tpapastylianou&#x2F;chain-ops-python" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tpapastylianou&#x2F;chain-ops-python</a><p>Same principle in octave: <a href="https:&#x2F;&#x2F;github.com&#x2F;tpapastylianou&#x2F;chain-ops-octave" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tpapastylianou&#x2F;chain-ops-octave</a>
kgwgk超过 4 年前
The lambda thing may be useful. Sometimes I was tempted to do something like this<p><pre><code> &gt; f &lt;- function(expr) eval(substitute( function(x) expr )) &gt; sapply(1:4, f({ a &lt;- x^2 ; b &lt;- x^3 ; a+b })) [1] 2 12 36 80 </code></pre> but I&#x27;m not sure it would have worked well. The new syntax is of course also more flexible.
评论 #25317753 未加载
clircle超过 4 年前
I think I&#x27;m going to appreciate the native pipes, it will likely improve the readability of my data.table chains.
评论 #25317506 未加载
arthurcolle超过 4 年前
This is awesome. Wonder if they picked up on that specific syntactic operator from Elixir or from another world.
stewbrew超过 4 年前
Well, afaik it isn&#x27;t really a pipe but syntactic sugar. A pipe streams data from one output stream to an input stream. This rewrites the code as if the input were passed as an argument.
评论 #25317617 未加载
评论 #25318097 未加载
r-w超过 4 年前
Julia crew, where you at?
评论 #25320673 未加载
f6v超过 4 年前
Better late than never, but dplyr solved the pipe problem long time ago.
评论 #25317159 未加载
评论 #25317100 未加载
评论 #25317132 未加载
评论 #25317101 未加载
tpoacher超过 4 年前
nitpicking, but this is not a lambda. it&#x27;s an anonymous function.
评论 #25321088 未加载
Huntsecker超过 4 年前
personally Im surprised R is still in active development when the main use case for people to use R (at least when I was using it) was for statistical analysis. Python with its libraries (a lot I believe ported from R) just does is nicer, and faster.
评论 #25317621 未加载
评论 #25317358 未加载
评论 #25317674 未加载
评论 #25317795 未加载
评论 #25319196 未加载