futureverse / futureverse/future
DOCUMENTATION: Pros & cons
- Dominant language
- R
- Stars
- 1k
- Forks
- 92
- PR merge metrics
- No merged PRs in 30d
Description
One advantage with where futures are used in for loop compared to `*apply()` alternatives is that the future expression may be used in only part of the for-loop expression.
For instance, in the below code, (a) `slow_calculation()` will not be called if all items have already been processed, and (b) it will only be processed once;
``` r
res <- listenv()
a <- NULL
for (ii in 1:nbr_of_items) {
if (is_done(ii)) next
if (is.null(a)) a <- slow_calculation()
res[[ii]] %<=% { process(ii, a=a) }
}
res <- as.list(res)
```
In order to achieve the same with an `*apply()` function, we need to split it up in at least two pieces, and do a two-pass scan over the data, e.g.
``` r
res <- list()
all_done <- all(sapply(1:nbr_of_items, FUN=is_done))
if (!all_done) {
a <- slow_calculation()
res <- lapply(1:nbr_of_items, FUN=process, a=a)
}
```
Contributor guide
Assessment
This issue has not been assessed yet.