From murdoch@dunc@n @end|ng |rom gm@||@com  Tue Dec  8 14:58:54 2020
From: murdoch@dunc@n @end|ng |rom gm@||@com (Duncan Murdoch)
Date: Tue, 8 Dec 2020 08:58:54 -0500
Subject: [Rd] [External] anonymous functions
In-Reply-To: <985b9b0c-69a2-c36e-24d6-294f13973ff5@gmail.com>
References: <28fddd$f2ifur@ironport10.mayo.edu>
 <alpine.DEB.2.21.2012071119120.3377@luke-Latitude-7480>
 <985b9b0c-69a2-c36e-24d6-294f13973ff5@gmail.com>
Message-ID: <d8e2c1b0-0faf-600b-7a07-db5050b9ccce@gmail.com>

On 08/12/2020 6:21 a.m., Duncan Murdoch wrote:
> 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, ...)

That's probably a bad choice of name for the generic in that it doesn't 
convert x to a call and it's only useful in a pipe, but I do think the 
idea of some generic function would be useful.

Duncan Murdoch

> 
> 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