abjur/abjutils

Problems in the with_*() family

clente opened this issue · 1 comments

In the latest commit (0c4c89d), I added the with_*() family of functions. They are supposed to wrap common functions in the purrr::map() family to add new functionalities to them: parallelism, progress tracking and side effect capturing.

Unfortunately, some things aren't working as expected 👎 Parallelism doesn't work with imap() because of the .y placeholder, and nothing works with map_if() because we don't deal with the predicate .p.

Should we simply ignore the situations where the functions don't work or should we try to deal with these edge cases?

# Setup
fun <- function(x, y, z) { x + y + z }
fun2 <- function(x, y, z) { Sys.sleep(1); x + y + z }
l <- list(1, 2, 3, "a", 4, 5)

# Parallel functions
par_map <- with_parallelism(purrr::map)
par_imap <- with_parallelism(purrr::imap)
par_map_if <- with_parallelism(purrr::map_if)

# Parallel tests
par_map(1:10, fun, y = 1, z = 2)
par_map(1:10, ~.x+1)
par_map(1:10, ~fun(1, .x, 2))
par_imap(1:10, fun, z = 2)
par_imap(1:10, ~.x+.y)                         ### COMPLETELY WRONG
par_imap(1:10, ~fun(.y, .x, 2))                ### COMPLETELY WRONG
par_map_if(1:10, ~.x%%2==0, fun, y = 1, z = 2) ### DOESN'T WORK
par_map_if(1:10, ~.x%%2==0, ~.x+1)             ### DOESN'T WORK
par_map_if(1:10, ~.x%%2==0, ~fun(1, .x, 2))    ### DOESN'T WORK

# Progress functions
pro_map <- with_progress(purrr::map)
pro_imap <- with_progress(purrr::imap)
pro_map_if <- with_progress(purrr::map_if)

# Progress tests
pro_map(1:10, fun2, y = 1, z = 2)
pro_map(1:10, ~{ Sys.sleep(1); .x+1 })
pro_map(1:10, ~fun2(1, .x, 2))
pro_imap(1:10, fun2, z = 2)
pro_imap(1:10, ~{ Sys.sleep(1); .x+.y })
pro_imap(1:10, ~fun2(.y, .x, 2))
pro_map_if(1:10, ~.x%%2==0, fun2, y = 1, z = 2)      ### NO PROGRESS
pro_map_if(1:10, ~.x%%2==0, ~{ Sys.sleep(1); .x+1 }) ### NO PROGRESS
pro_map_if(1:10, ~.x%%2==0, ~fun2(1, .x, 2))         ### NO PROGRESS

# Possibly functions
pos_map <- with_possibly(purrr::map)
pos_imap <- with_possibly(purrr::imap)
pos_map_if <- with_possibly(purrr::map_if)

# Possibly tests
pos_map(l, fun, y = 1, z = 2)
pos_map(l, ~.x+1)
pos_map(l, ~fun(1, .x, 2))
pos_imap(l, fun, z = 2)
pos_imap(l, ~.x+.y)
pos_imap(l, ~fun(.y, .x, 2))
pos_map_if(l, ~.x%%2==0, fun, y = 1, z = 2) ### DOESN'T WORK
pos_map_if(l, ~.x%%2==0, ~.x+1)             ### DOESN'T WORK
pos_map_if(l, ~.x%%2==0, ~fun(1, .x, 2))    ### DOESN'T WORK

This family was abandoned.