From murdoch@dunc@n @end|ng |rom gm@||@com  Tue Dec  8 12:21:57 2020
From: murdoch@dunc@n @end|ng |rom gm@||@com (Duncan Murdoch)
Date: Tue, 8 Dec 2020 06:21:57 -0500
Subject: [Rd] [External] anonymous functions
In-Reply-To: <alpine.DEB.2.21.2012071119120.3377@luke-Latitude-7480>
References: <28fddd$f2ifur@ironport10.mayo.edu>
 <alpine.DEB.2.21.2012071119120.3377@luke-Latitude-7480>
Message-ID: <985b9b0c-69a2-c36e-24d6-294f13973ff5@gmail.com>

On 07/12/2020 12:26 p.m., luke-tierney at uiowa.edu wrote:
> I don't disagree in principle, but the reality is users want shortcuts
> and as a result various packages, in particular tidyverse, have been
> providing them. Mostly based on formulas, mostly with significant
> issues since formulas weren't designed for this, and mostly
> incompatible (tidyverse ones are compatible within tidyverse but not
> with others). And of course none work in sapply or lapply. Providing a
> shorthand in base may help to improve this. You don't have to use it
> if you don't want to, and you can establish coding standards that
> disallow it if you like.
Here's a suggestion to let people define their own shorthands and work 
with the current |> definition.

Define "as.call" as an S3 generic, with a default definition something like

as.call.default <- function(x, f, ...) f(x, ...)

so one could use

  x |> as.call(mean)

to get the same result as

  x |> mean()

or (if working in the tidyverse)

  x |> as.call(~ .x + 1)

to use a method to be provided by rlang or purrr to convert their 
shorthand into a call that the pipe can work with.

We already have the generic as.function, which could be used internally 
by lapply and sapply for the same sort of purpose.  tidyverse has 
rlang::as_function, so they could pretty easily add methods for 
as.function if they wanted to allow people to use their shorthand in 
*apply functions.

Duncan Murdoch



[Rd] [External] anonymous functions
Duncan Murdoch