tidyverse / magrittr

Improve the readability of R code with the pipe
https://magrittr.tidyverse.org
Other
959 stars 157 forks source link

`magrittr::freduce()` doesn't support lambda functions #250

Closed emmansh closed 3 years ago

emmansh commented 3 years ago

Why doesn't magrittr::freduce() support lambda functions?

library(magrittr)

my_3_operations_anon <- 
  list(
  function(x) mean(x),
  function(x) sqrt(x),
  function(x) log(x)
)

my_3_operations_lambda <- 
  list(
    ~mean(.),
    ~sqrt(.),
    ~log(.)
  )

magrittr::freduce(1:10, my_3_operations_anon)
#> [1] 0.852374
magrittr::freduce(1:10, my_3_operations_lambda)
#> Error in function_list[[i]](value): attempt to apply non-function

Created on 2021-10-26 by the reprex package (v2.0.1)

lionel- commented 3 years ago

Now that base R also has syntax for lambda functions there's not as much benefit in supporting tidyverse lambdas.

Also magrittr currently has zero dependencies and we probably don't want to add one just for that. rlang is already in Suggests so we could check for a formula, check that rlang is installed, and then call rlang::as_function().

You're welcome to send a PR but there is no release planned in the immediate future. It might be years until that makes it to CRAN in the very worst case, at which point the transition to native lambdas will have made much progress.

emmansh commented 3 years ago

@lionel- thanks. Could you demonstrate how you'd use rlang::as_function() in this example (i.e., assuming it's installed)?

lionel- commented 3 years ago

oh if you just want to transform on your side then it's simply:

lapply(my_3_operations_lambda, rlang::as_function)
emmansh commented 3 years ago

OK then, I'll live with that hack :)

Thanks.