From murdoch@dunc@n @end|ng |rom gm@||@com  Mon Dec  7 18:29:24 2020
From: murdoch@dunc@n @end|ng |rom gm@||@com (Duncan Murdoch)
Date: Mon, 7 Dec 2020 12:29:24 -0500
Subject: [Rd] [External] Re: New pipe operator
In-Reply-To: <CAKorm_uh1pbJ8pOr0m7MYvE6kDCYETBXAQ1ogzc-g1nvy8d52A@mail.gmail.com>
References: <e55c2093-0bb5-5f9b-5521-0ca8bdf35953@gmail.com>
 <CALyqOb8c1hHOSaMV+fqXTwpeOsP--KJ_t-oeqMYYafVXeNpqZg@mail.gmail.com>
 <7586e034-e287-0970-6480-f82d747b5445@gmail.com>
 <CADfFDC7bT58_+52uvmpHuMMQRNbbmqV3_7t_fc4sgJ_Esw=xag@mail.gmail.com>
 <CAJmOi+PO_8XDC23=rT=pQ76ENL=gx2V-41ax2fHSVs87xURcEA@mail.gmail.com>
 <alpine.DEB.2.21.2012051203250.3003@luke-Latitude-7480>
 <CAP01uRmd0tRM4xYnegK=YijJv6htqq+fuOwBMBns--0+2a7Opw@mail.gmail.com>
 <alpine.DEB.2.21.2012070908010.3377@luke-Latitude-7480>
 <CAP01uRmP=1vtHUeqS6yAX1BeP1am_2auZCEFbgzm6kb_nafDzA@mail.gmail.com>
 <CAKorm_uh1pbJ8pOr0m7MYvE6kDCYETBXAQ1ogzc-g1nvy8d52A@mail.gmail.com>
Message-ID: <af5fbe49-5261-2360-0073-d1fcd058936e@gmail.com>

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



[Rd] [External] Re: New pipe operator
Duncan Murdoch