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

map and lazy evaluation #191

Closed
mplourde opened this issue Apr 14, 2016 · 4 comments

Comments

@mplourde
Copy link

commented Apr 14, 2016

library(purrr)
packageVersion('purrr')
# [1] '0.2.1.9000'

This is an issue I encountered with newer releases of purrr when using map to wrap functions. map in prior versions of purrr didn't have this issue, but I'm not sure when the change occurred. Consider the following example where I create a list of functions and then wrap them with lapply:

funs <- list(
    function() message('fun1'),
    function() message('fun2'),
    function() message('fun3')
)

wrapper <- function(fun) {
    function() fun()
}

. <- funs %>%
    lapply(wrap_fun) %>% 
    invoke_map()
# fun1
# fun2
# fun3

When I perform this task with map:

. <- funs %>% 
    map(wrapper) %>%
    invoke_map()
# fun3
# fun3
# fun3

forceing the fun argument of wrapper fixes the issue:

forced_wrapper <- function(fun) {
    force(fun)
    function() fun()
}

. <- funs %>% 
    map(forced_wrapper) %>%
    invoke_map()
# fun1
# fun2
# fun3

Since map basically supplants lapply, it would be helpful if they behaved the same in this respect.

@shichien

This comment has been minimized.

Copy link

commented May 6, 2016

First of all, thanks @hadley for writing such wonderful package. I second this issue and can provide another (simpler) example.

Very often I want to make a list of functions that depends on some varying parameters. For example, making a list of functions that print out 1..5.

> f <- map(1:5, function(i) function() print(i))

However, if I try to call the first one, it gives 5 instead of 1.

> f[[1]]()
[1] 5

This contradicts to my intuition with functional programming and lexical scoping, and for the unaware, can lead to serious bugs in code.

With a bit of searching, I found one workaround is to make another factory function and explicitly reference i in its body, such as:

> make_f <- function(i) { i; function() print(i) }
> f <- map(1:5, make_f)
> f[[1]]()
[1] 1

But I think we don't have the issue with lapply:

> f <- lapply(1:5, function(i) function() { print(i) })
> f[[1]]()
[1] 1
@hadley

This comment has been minimized.

Copy link
Member

commented Jan 24, 2017

This is a consequence of R's delayed evaluation - however lapply() somehow works around it so we should too.

@jimhester

This comment has been minimized.

Copy link
Member

commented Feb 24, 2017

For reference the R-devel thread where this was changed in lapply is https://stat.ethz.ch/pipermail/r-devel/2015-February/070686.html

@hadley

This comment has been minimized.

Copy link
Member

commented Mar 5, 2017

Implemented in wch/r-source@14f904c

@hadley hadley closed this in b041e78 Mar 7, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.