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

Pipes of functions have broken environments #159

Closed
HuwCampbell opened this issue Dec 14, 2017 · 4 comments
Closed

Pipes of functions have broken environments #159

HuwCampbell opened this issue Dec 14, 2017 · 4 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@HuwCampbell
Copy link

Sorry if this appears a little obscure, it's causing me some difficulty.

I found this issue after you posted a review for hedgehog (ta!). Using the pipe I was having infinite recursion due to the environments being brought into functions incorrectly.

Here's a quick bug showing something similar

compose <- function(f, g) { function(x) g(f(x)) }
plus1   <- function(x) x + 1
compose(plus1, plus1)(5)
# [1] 7
plus2 <- plus1 %>% compose(plus1)
plus2(5)
# Error in g(f(x)) : could not find function "f"
plus2
# function(x) g(f(x))
# <environment: 0x7f977ecd1ea8>
as.list(environment(plus2))
# $f
# $f$value
# function (x) 
# g(f(x))
# <environment: 0x7f977ecd1ea8>
# 
# $f$visible
# [1] TRUE

# $g
# function (x) 
# x + 1

See above that f$value refers to g(f(x)), when it should be function(x) x + 1.

@HuwCampbell
Copy link
Author

Might be a duplicate of #146 but I can't tell

@stefanbache
Copy link

Noted. It does work in the update branch, which uses a different approach. I'm behind on giving this attention, hope to get some time for this soon.

@lionel-
Copy link
Member

lionel- commented Jul 23, 2020

This is not a problem of environment but of lazy evaluation. Forcing the inputs solves the problem:

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

plus2 <- plus1 %>% compose(plus1)
plus2(5)
#> [1] 7

See also #195.

@lionel-
Copy link
Member

lionel- commented Aug 3, 2020

Fixed by #218

@lionel- lionel- closed this as completed Aug 3, 2020
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
Projects
None yet
Development

No branches or pull requests

4 participants