You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, I've noticed a difference in behavior between the possibly() and slowly() functions in the purrr package when attempting to use them in anonymous functions in map_*() and walk().
Problem
When using possibly(), I can create a new function on the fly using an anonymous function, and it works as expected. The encapsulation of the original function and its arguments is preserved, allowing me to apply the effect of possibly() seamlessly.
However, when attempting to do the same with slowly(), the delay effect of slowly() seems to be lost when creating the new function on the fly using an anonymous function. The encapsulation of the effect is not preserved, and the delay is not added to the execution.
Expected Behavior
I expected the behavior of slowly() to be consistent with that of possibly(), where the effect of slowly() is properly encapsulated and applied when creating new functions on the fly using anonymous functions. Is this behavior the intended one ? Is there something I'm missing in my code ? If it is the intended behavior, is there a way to achieve the desired effect with slowly() as with possibly() ?
Thank you
Reprex
library(purrr)
#> Warning: le package 'purrr' a été compilé avec la version R 4.2.3# --- 2 Listsgood_list=list(1, 2, 3, 4, 5)
error_list=list("abc", NULL, 3, 4, "def")
# --- Define printlogprintlog=function(x) print(log(x))
# possibly() Works as expected --------------------------------------------# I use possibly() to create a new functionpossibly_printlog= possibly(printlog, otherwise=0)
# that I can use with map_* and walk.result= map_dbl(good_list , possibly_printlog)
#> [1] 0#> [1] 0.6931472#> [1] 1.098612#> [1] 1.386294#> [1] 1.609438result= map_dbl(error_list, possibly_printlog)
#> [1] 1.098612#> [1] 1.386294
walk(good_list , possibly_printlog)
#> [1] 0#> [1] 0.6931472#> [1] 1.098612#> [1] 1.386294#> [1] 1.609438
walk(error_list, possibly_printlog)
#> [1] 1.098612#> [1] 1.386294# I can also create the new function on the fly and it works the sameresult= map_dbl(good_list , \(x) possibly(printlog, otherwise=0) (x))
#> [1] 0#> [1] 0.6931472#> [1] 1.098612#> [1] 1.386294#> [1] 1.609438result= map_dbl(error_list, \(x) possibly(printlog, otherwise=0) (x))
#> [1] 1.098612#> [1] 1.386294
walk(good_list , \(x) possibly(printlog, otherwise=0)(x))
#> [1] 0#> [1] 0.6931472#> [1] 1.098612#> [1] 1.386294#> [1] 1.609438
walk(error_list, \(x) possibly(printlog, otherwise=0)(x))
#> [1] 1.098612#> [1] 1.386294# slowly() Doesn't work as expected ---------------------------------------# I use slowly() to create a new functionslowly_printlog= slowly(printlog, rate= rate_delay(0.5))
# that I can use with map_* and walk.result= map_dbl(good_list, slowly_printlog) # Works fine#> [1] 0#> [1] 0.6931472#> [1] 1.098612#> [1] 1.386294#> [1] 1.609438
walk(good_list, slowly_printlog) # Works fine#> [1] 0#> [1] 0.6931472#> [1] 1.098612#> [1] 1.386294#> [1] 1.609438# However, I can't create the new function on the fly as I did with possibly()# --> The delay is not addedresult= map_dbl(
good_list,
\(x) slowly(printlog, rate= rate_delay(0.5)) (x)
)
#> [1] 0#> [1] 0.6931472#> [1] 1.098612#> [1] 1.386294#> [1] 1.609438
walk(
good_list,
\(x) slowly(printlog, rate= rate_delay(0.5)) (x)
)
#> [1] 0#> [1] 0.6931472#> [1] 1.098612#> [1] 1.386294#> [1] 1.609438
Description
Hello, I've noticed a difference in behavior between the possibly() and slowly() functions in the purrr package when attempting to use them in anonymous functions in map_*() and walk().
Problem
When using possibly(), I can create a new function on the fly using an anonymous function, and it works as expected. The encapsulation of the original function and its arguments is preserved, allowing me to apply the effect of possibly() seamlessly.
However, when attempting to do the same with slowly(), the delay effect of slowly() seems to be lost when creating the new function on the fly using an anonymous function. The encapsulation of the effect is not preserved, and the delay is not added to the execution.
Expected Behavior
I expected the behavior of slowly() to be consistent with that of possibly(), where the effect of slowly() is properly encapsulated and applied when creating new functions on the fly using anonymous functions. Is this behavior the intended one ? Is there something I'm missing in my code ? If it is the intended behavior, is there a way to achieve the desired effect with slowly() as with possibly() ?
Thank you
Reprex
Session info
The text was updated successfully, but these errors were encountered: