Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce applied to a list of functions #643

Closed
neuwirthe opened this issue Feb 19, 2019 · 5 comments
Closed

reduce applied to a list of functions #643

neuwirthe opened this issue Feb 19, 2019 · 5 comments
Labels
bug an unexpected problem or unintended behavior reduce 🔨

Comments

@neuwirthe
Copy link

reduce does not work in the following example.
Should reduce applied to a lust of functions work this way?

compose <- function(f,g){
  function(x)f(g(x))
}
compose(sin,sqrt)(2)
reduce(list(sin,sqrt),compose,.dir="backward")(2)
reduce(list(sin,sqrt),compose,.dir="forward")(2)
reduce(list(sin,sqrt),compose,.dir="forward") %>% class()
reduce(list(sin,sqrt),compose,.dir="backward") %>% class()
@batpigandme
Copy link
Contributor

When I try to run this, I'm getting the following error after trying to run the first reduce().

#> Error: C stack usage  7969232 is too close to the limit

Could you please turn this into a self-contained reprex (short for minimal reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff.

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page.

You can install reprex by running (you may already have it, though, if you have the tidyverse package installed):

install.packages("reprex")

Thanks

@neuwirthe
Copy link
Author

The error message you get is exactly why I wrote the post.
So, you have reproduced the error yourself ;-)

@batpigandme
Copy link
Contributor

batpigandme commented Feb 19, 2019

OK. For future reference, it's helpful to know what error you're encountering for triaging/figuring out issues. reprex (which shows input and output code) doesn't work in this case anyhow, because of the C stack error, but including the error that you are getting in the issue makes things just a little bit easier to get sorted!

@moodymudskipper
Copy link
Contributor

If you use purr::compose it will work just fine.

I don't fully understand the mechanism but you can fix your function by forcing the evaluation of f in compose.

compose <- function(f,g){
  force(f)
  function(x){
    f(g(x))
  }
}
reduce(list(sin,sqrt),compose)(2)
# [1] 0.9877659

Still I guess it can be considered a purrr issue because it works fine with your former function if using base::Reduce :

compose <- function(f,g){
  function(x){
    f(g(x))
  }
}

Reduce(compose, list(sin,sqrt))(2)
# [1] 0.9877659

@egnha
Copy link
Contributor

egnha commented Feb 20, 2019

@neuwirthe, reduce() isn't required to compose a list of functions. (In fact, doing so adds some overhead to the resulting composition.) It is more idiomatic to use unquote-splicing to compose a list of functions, e.g.

fns <- list(sin, cos)
compose(!!! fns)

@lionel- lionel- added bug an unexpected problem or unintended behavior reduce 🔨 labels Feb 27, 2019
lionel- added a commit to lionel-/lowliner that referenced this issue Feb 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior reduce 🔨
Projects
None yet
Development

No branches or pull requests

5 participants