Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is right-to-left operator associativity in R possible?

Tags:

I'm new to R, and I just discovered I suffer from Bracket Phobia (see comment in the link). I like the way magrittr notation %>% works, because it avoids nested parenthesis in some situations, and makes code more readable. I came from Mathematica, where there is a very similar native // notation to do what %>% does. Here are some R and Mathematica comparisons:

#R Notation     c(1.5,-2.3,3.4) %>% round %>% abs %>% sum   

#Mathematica Notation {1.5,-2.3,3.4}//Round//Abs//Total 

So far so good, but, my question is:

Is there some way to mimic Mathematica @ notation, with right-to-left associativity in R?

Here is how it works in Mathematica, to solve the same code above:

Total@Abs@Round@{1.5,-2.3,3.4} 

In Mathematica it can also be write as:

Total[Abs[Round[{1.5,-2.3,3.4}]]] 

just like in R it would be:

sum(abs(round(c(1.5,-2.3,3.4)))) 

But it would be much more clean (and cool) to have in R something like this:

sum@abs@round@c(1.5,-2.3,3.4) 

PS: I know @ is used in S4 classes, and is not a good idea. This is just an illustrative comparison.

like image 373
Murta Avatar asked Jul 08 '15 23:07

Murta


Video Answer


2 Answers

I haven't tested/thought about this carefully at all, but defining function composition via an operator (as below) seems to work in a couple of test cases:

library(magrittr)  ## operator defined as  "left-pointing arrow" at the  ##    suggestion of @ClausWilke: "%<%" <- function(x,y) { if (is(y,"function"))            function(z) x(y(z))        else x(y) }  x <- c(1.5,-2.3,3.4) all.equal(x %>% round %>% abs %>% sum,           sum %<% abs %<% round %<% x)  x <- rnorm(1000) all.equal(x %>% round %>% abs %>% sum,           sum %<% abs %<% round %<% x) 

The syntax is not as nice as being able to use a single character (such as @) for the composition operator, but even if you could find an unused one (all the obvious ones [!@#$%^&*~|] are taken, and masking them would be a terrible idea) there's a parser limitation: user-defined binary operators in R must be of the form %?%.

like image 192
Ben Bolker Avatar answered Sep 18 '22 14:09

Ben Bolker


How about using compose from hadley's purrr package?

compose(sum,abs,round,c)(1.5,-2.3,3.4) 
like image 38
Neal Fultz Avatar answered Sep 21 '22 14:09

Neal Fultz