tidyverse / purrr

A functional programming toolkit for R
https://purrr.tidyverse.org/
Other
1.27k stars 271 forks source link

Feature idea: API for accessing adverb's underlying function(s) #1071

Closed mmuurr closed 1 year ago

mmuurr commented 1 year ago

At times I find it's useful to retain the original function when passed to an adverb, such as possibly(). Obviously, when the original function is available by direct reference, this is a non-issue:

orig_f <- function(...) { do_some_stuff };
new_f <- purrr::possibly(f)

so_something_with(orig_f)  ## still have access to orig_f

But when using adverbs in a slightly more functional (and less-declarative) manner, I find I like to do something like this:

the_only_f <- (function(...) { do_some_stuff }) |> purrr::possibly()

... and when wanting to do something with the original function I do (something along the lines of):

environment(the_only_f)$.f

That ^ approach is a bit of a leaky abstraction, so I'm wondering if there's appetite for an accessor like:

get_wrapped_fun(the_only_f)

The only {purrr} adverb where that example API wouldn't really work is compose(), though a more generic:

get_wrapped_funs(adverbed_f)

... could yield a list of functions.

Just a thought, as whenever I find myself plucking objects out of a function's environment I think to myself, "this is unsafe" :-)

Cheers!

hadley commented 1 year ago

Thanks for filing this issue! Unfortunately, while I think it's an interesting idea, I think it's out of scope for this package: developing good software requires relentless focus, which means that we have to say no to many good ideas. Even though I'm closing this issue, I really appreciate the feedback, and hope you'll continue to contribute in the future 😄