Skip to content
Prev 59300 / 63424 Next

New pipe operator

On 08-12-2020 12:46, Gabor Grothendieck wrote:
I think only allowing functions on the right hand side (e.g. only the |> 
operator and not the |:>) would be enough to handle most cases and seems 
easier to reason about. The limitations of that can easily be worked 
around using existing functionality in the language.

The problem with only allowing

x |> mean

and not

x |> mean()

is with additional arguments. However, this can be solved with a 
currying function, for example:

x |> curry(mean, na.rm = TRUE)

The cost is a few additional characters.

In the same way it is possible to write a function that accepts an 
expression and returns a function containing that expression. This can 
be used to have expressions on the right-hand side and reduces the need 
for anonymous functions.

x |> fexpr(. + 10)
dta |> fexpr(lm(y ~ x, data = .))

You could call this function .:

x |> .(. + 10)
dta |> .(lm(y ~ x, data = .))


Dummy example code (thanks to  a colleague of mine)


fexpr <- function(expr){
   expr <- substitute(expr)
   f <- function(.) {}
   body(f) <- expr
   f
}
. <- fexpr

curry <- function(fun,...){
   L <- list(...)
   function(...){
     do.call(fun, c(list(...),L))
   }
}

`%|>%` <- function(e1, e2) {
   e2(e1)
}


1:10 %>% mean
c(1,3,NA) %|>% curry(mean, na.rm = TRUE)
c(1,3,NA) %|>% .( mean(., na.rm = TRUE) ) %>% identity
c(1,3,NA) %|>% .( . + 4)
c(1,3,NA) %|>% fexpr( . + 4)
c(1,3,NA) %|>% function(x) mean(x, na.rm = TRUE) %>% fexpr(. + 1)

--
Jan

Thread (78 messages)

Duncan Murdoch New pipe operator Dec 4 Hiroaki Yutani New pipe operator Dec 4 Duncan Murdoch New pipe operator Dec 4 Dénes Tóth New pipe operator Dec 4 Sebastian Meyer New pipe operator Dec 4 Deepayan Sarkar New pipe operator Dec 4 iuke-tier@ey m@iii@g oii uiow@@edu New pipe operator Dec 4 Duncan Murdoch New pipe operator Dec 4 Duncan Murdoch New pipe operator Dec 4 iuke-tier@ey m@iii@g oii uiow@@edu New pipe operator Dec 4 Avi Gross New pipe operator Dec 4 Ivan Krylov New pipe operator Dec 4 Duncan Murdoch New pipe operator Dec 5 Hugh Parsonage New pipe operator Dec 5 Gabor Grothendieck New pipe operator Dec 5 iuke-tier@ey m@iii@g oii uiow@@edu New pipe operator Dec 5 John Mount New pipe operator Dec 5 Abby Spurdle New pipe operator Dec 5 Gregory R. Warnes New pipe operator Dec 5 Hiroaki Yutani New pipe operator Dec 5 Rui Barradas New pipe operator Dec 5 Jan Gorecki New pipe operator Dec 6 Dénes Tóth New pipe operator Dec 6 Avi Gross New pipe operator Dec 6 Duncan Murdoch New pipe operator Dec 6 Dénes Tóth New pipe operator Dec 6 Duncan Murdoch New pipe operator Dec 6 Gabor Grothendieck New pipe operator Dec 6 iuke-tier@ey m@iii@g oii uiow@@edu New pipe operator Dec 6 Gabor Grothendieck New pipe operator Dec 6 Avi Gross New pipe operator Dec 6 iuke-tier@ey m@iii@g oii uiow@@edu New pipe operator Dec 6 Gabriel Becker New pipe operator Dec 6 Dénes Tóth New pipe operator Dec 6 Gabor Grothendieck New pipe operator Dec 6 Gabriel Becker New pipe operator Dec 6 Gabor Grothendieck New pipe operator Dec 6 Bravington, Mark (Data61, Hobart) New pipe operator Dec 6 Gabriel Becker New pipe operator Dec 6 Gabor Grothendieck New pipe operator Dec 6 Duncan Murdoch New pipe operator Dec 7 Duncan Murdoch New pipe operator Dec 7 Gabor Grothendieck New pipe operator Dec 7 Gabor Grothendieck New pipe operator Dec 7 iuke-tier@ey m@iii@g oii uiow@@edu New pipe operator Dec 7 Deepayan Sarkar New pipe operator Dec 7 Gabor Grothendieck New pipe operator Dec 7 Deepayan Sarkar New pipe operator Dec 7 Peter Dalgaard New pipe operator Dec 7 Gabor Grothendieck New pipe operator Dec 7 Duncan Murdoch New pipe operator Dec 7 Gregory R. Warnes New pipe operator Dec 7 Peter Dalgaard New pipe operator Dec 7 Duncan Murdoch New pipe operator Dec 7 Duncan Murdoch New pipe operator Dec 7 iuke-tier@ey m@iii@g oii uiow@@edu New pipe operator Dec 7 Gabor Grothendieck New pipe operator Dec 7 Kevin Ushey New pipe operator Dec 7 Gabor Grothendieck New pipe operator Dec 7 Gabriel Becker New pipe operator Dec 7 Gabriel Becker New pipe operator Dec 7 Dénes Tóth New pipe operator Dec 7 Gabriel Becker New pipe operator Dec 7 Gabor Grothendieck New pipe operator Dec 8 Hadley Wickham New pipe operator Dec 8 Gabor Grothendieck New pipe operator Dec 8 Bravington, Mark (Data61, Hobart) New pipe operator Dec 8 Duncan Murdoch New pipe operator Dec 8 Bravington, Mark (Data61, Hobart) New pipe operator Dec 8 Jan van der Laan New pipe operator Dec 9 Duncan Murdoch New pipe operator Dec 9 Duncan Murdoch New pipe operator Dec 9 Jan van der Laan New pipe operator Dec 9 Duncan Murdoch New pipe operator Dec 9 Gabor Grothendieck New pipe operator Dec 9 Gabriel Becker New pipe operator Dec 9 Gabor Grothendieck New pipe operator Dec 9 Peter Dalgaard New pipe operator Dec 9