<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/in-praise-of-syntactic-sugar/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2017/07/in-praise-of-syntactic-sugar/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>There has been some talk of adding native pipe notation to <a rel="nofollow" href="https://cran.r-project.org/" target="_blank">R</a> (for example <a rel="nofollow" href="http://lionel-.github.io/2016/02/15/ideas-for-an-updated-r-syntax/" target="_blank">here</a>, <a rel="nofollow" href="https://rawgit.com/jimhester/presentations/master/2017_07_03-DSC2017-Syntax_Extensions_To_R/2017_07_03-DSC2017-Syntax_Extensions_To_R.html" target="_blank">here</a>, and <a rel="nofollow" href="https://twitter.com/groundwalkergmb/status/882966640891011072" target="_blank">here</a>).</p>
<p>I think a critical aspect of such an extension would be to treat such a notation as <a rel="nofollow" href="https://en.wikipedia.org/wiki/Syntactic_sugar" target="_blank"><em>syntactic sugar</em></a> and <em>not</em> insist such a pipe match <a rel="nofollow" href="https://cran.r-project.org/package=magrittr" target="_blank">magrittr</a> semantics, or worse yet give a platform for authors to insert their own preferred ad-hoc semantics.<span></span></p>
<p>
<p>A prominent place where <a rel="nofollow" href="http://theburningmonk.com/2011/09/fsharp-pipe-forward-and-pipe-backward/" target="_blank">pipe-notation is used is in the language F#,</a>, where it is literally defined in F# itself as:</p>
<pre>
    let (|>) x f = f x
</pre>
<p>From this simple definition, one versed in the semantics of F# has a chance at inferring the semantics of the pipe.</p>
<p>In the R language, the magrittr package supplies a pipe written as “%>%”. This pipe’s implementation depends on very complicated unevaluated expression capture and direct manipulation of execution environments. Also the magrittr pipe picks its own semantics as it wants, as it does not inherit semantics from a simple definition. For example all of the following are valid magrittr:</p>
<pre>
library(&quot;magrittr&quot;)

4 %>% sin
#> [1] -0.7568025

4 %>% sin()
#> [1] -0.7568025

4 %>% sin(.)
#> [1] -0.7568025
</pre>
<p>These all seem to be convenient choices, but new users have to memorize them as they can not infer these from things they may already know about the R programming language.</p>
<p>Also the freedom of choice in semantics means many arbitrary choices get made (nothing is prior apodictic) and you get some debatable choices such as: “<code>data.frame(x=1) %>% dplyr::bind_rows(list(.,.))</code>” having 3 rows.</p>
<p>What I would like if a native pipe were to be added to R is: for the pipe to be defined as formally equivalent to some larger R expression. Then we could teach it as such and we would not get any new corner cases or exceptional behaviors. Even if an implementation fails to reach this standard, this way of defining things lets us now how it <em>should</em> have worked (in a perfect world).</p>
<p>A good R pipe operator might have an aspirational definition along the lines of:</p>
<pre>
 &quot;a %.>% b&quot; is to be treated:
  as if the user had written &quot;{ . &lt;- a; b };&quot;
  with &quot;%>%&quot; being treated as left-associative,
  and .-side effects removed.
</pre>
<p>Notice we are not saying “<code>b(a)</code>” as that doesn’t deal with repeated use of arguments and other cases R-users have come to expect. This also allows piping into non-function expressions (a neat feature).</p>
<p>Surprisingly enough the above actually works. It means a pipeline of such as the following:</p>
<pre>
a &lt;-
  4      %.>%
  sin(.) %.>%
  exp(.) %.>%
  cos(.)
</pre>
<p>Is unambiguously meant to be a short-hand for the following (ugly) nested code.</p>
<pre>
a &lt;- { . &lt;- { . &lt;- { . &lt;- 
    4       ; 
    sin(.) }; 
    exp(.) }; 
    cos(.) };
</pre>
<p>It does not matter that nobody would write the nested code, that is precisely what we are not asking anybody to do. The point is, a student can attempt to check this translation on small examples, and even run both versions.</p>
<p>Notice in the above ugly nested example that “<code>};</code>” is starting to look a bit like a piping operator. This is calling out two things:</p>
<ul>
<li>Piping syntax largely a convention of using (possibly anonymous) intermediate values instead of nesting of calls.</li>
<li>Piping semantics are largely about sequencing statements, this is the usual <a rel="nofollow" href="http://www.win-vector.com/blog/2016/12/magrittrs-doppelganger/" target="_blank">monad is the expensive way to say “programmable semicolon” observation.</a></li>
</ul>
<p>Roughly if the user is willing to write code such as the following, then they don’t need pipes.</p>
<pre>
     . &lt;-  4 
     . &lt;-  sin(.)
     . &lt;-  exp(.)
     . &lt;-  cos(.)
a &lt;- .
</pre>
<p>Or, as we have observed before, some notations start to look like you already have piping capabilities in base-R (arguing one should give base-R a chance before insisting on extensions).</p>
<p>For example we can use “<code>;.&lt;-</code>” as a pipe (the first one I noticed, and my attempt to not use left-arrow that often):</p>
<pre>
a   &lt;-{ 
  4        ->.;.&lt;- 
  sin(.)   ;.&lt;-
  exp(.)   ;.&lt;-
  cos(.)   ;.}
</pre>
<p>Or we can use “<code>->.;</code>” as a pipe:</p>
<pre>
a   &lt;-{ 
  4        ->.;
  sin(.)   ->.; 
  exp(.)   ->.;
  cos(.)   }
</pre>
<p>I think this last one is actually pretty if we go all-in with right-assignment:</p>
<pre>
  4      ->.; 
  sin(.) ->.; 
  exp(.) ->.; 
  cos(.) ->    a
</pre>
<p>The remaining strong objections to this <a rel="nofollow" href="https://cran.r-project.org/web/packages/replyr/vignettes/BizarroPipe.html" target="_blank">Bizarro Pipe</a> notation (in my mind) are:</p>
<ul>
<li>“<code>->.;</code>” is an ugly glyph. This is because it’s representation <em>is</em> its implementation.</li>
<li>This pipe isn’t very compatible with left-assignment (R’s prefrered assignment) without adding additional blocks.</li>
</ul>
<p>Roughly: introducing new notation need not be as disruptive as introducing new semantics. Also conventions can have great advantage, even if they do not have language assistance or enforcement (though such things are good).</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.win-vector.com/blog/2017/07/in-praise-of-syntactic-sugar/"> R – Win-Vector Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/C8xB_7rGRIE" height="1"></a></div>