[Rd] [External] Re: New pipe operator
Duncan Murdoch
murdoch@dunc@n @end|ng |rom gm@||@com
Mon Dec 7 18:29:24 CET 2020
On 07/12/2020 12:03 p.m., Gregory Warnes wrote:
> My vote is for the consistency of function calls always having parentheses,
> including in pipes. Making them optional only saves two keystrokes, but
> will add yet another inconsistency to confuse or trip folks up.
>
> As for the new anonymous function syntax, I would prefer something more
> human friendly, perhaps provide “fun” as a shortcut for “function”,
> enabling:
>
> DF <- "myfile.csv" %>%
> readLines() |>
> fun(x) gsub(r'{(c\(.*?\)|integer\(0\))}', r'{"\1"}', x) |>
> fun(x) read.csv(text = x)|>
> mutate(
> across(2:3,
> fun(col) lapply(col,
> fun(x) eval(parse(text = x))
> )
> )
> )
>
> which seems much easier to read and understand, at the cost of only a few
> extra characters.
But you didn't "always" include parentheses, you skipped them on the
calls to the anonymous functions. I think that's the one place I'd make
the exception, so maybe we agree: parens are almost always needed, with
the sole exception being anonymous functions.
As to using "fun", I think that's a bad idea. I haven't checked, but I
wouldn't be too surprised if "fun" has been used thousands of times in
CRAN packages as the name of a function. So
x |> fun(y)
would mean "fun(x, y)", whereas
x |> fun(y) y+1
would mean (function(y) y+1)(x).
Duncan Murdoch
More information about the R-devel
mailing list