Skip to content

Commit d3e1e9b

Browse files
authored
Add future_map_vec() and friends (#300)
1 parent a74d203 commit d3e1e9b

17 files changed

+746
-0
lines changed

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export(future_imap_dfc)
2525
export(future_imap_dfr)
2626
export(future_imap_int)
2727
export(future_imap_lgl)
28+
export(future_imap_vec)
2829
export(future_invoke_map)
2930
export(future_invoke_map_chr)
3031
export(future_invoke_map_dbl)
@@ -41,6 +42,7 @@ export(future_map2_dfc)
4142
export(future_map2_dfr)
4243
export(future_map2_int)
4344
export(future_map2_lgl)
45+
export(future_map2_vec)
4446
export(future_map_at)
4547
export(future_map_chr)
4648
export(future_map_dbl)
@@ -49,6 +51,7 @@ export(future_map_dfr)
4951
export(future_map_if)
5052
export(future_map_int)
5153
export(future_map_lgl)
54+
export(future_map_vec)
5255
export(future_modify)
5356
export(future_modify_at)
5457
export(future_modify_if)
@@ -59,6 +62,7 @@ export(future_pmap_dfc)
5962
export(future_pmap_dfr)
6063
export(future_pmap_int)
6164
export(future_pmap_lgl)
65+
export(future_pmap_vec)
6266
export(future_pwalk)
6367
export(future_walk)
6468
export(future_walk2)

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
## Features / Fixes
1313

14+
* New `future_map_vec()`, `future_map2_vec()`, `future_pmap_vec()`, and
15+
`future_imap_vec()` to align with purrr (#261).
16+
1417
* furrr now looks up the purrr mapping function on the worker itself, rather
1518
than sending over its own copy of the function. This avoids possible issues
1619
when you have, say, purrr 1.0.0 locally but purrr 0.3.5 on the worker, where

R/future-imap.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,29 @@ future_imap_lgl <- function(
129129
)
130130
}
131131

132+
#' @rdname future_imap
133+
#' @export
134+
future_imap_vec <- function(
135+
.x,
136+
.f,
137+
...,
138+
.ptype = NULL,
139+
.options = furrr_options(),
140+
.env_globals = parent.frame(),
141+
.progress = FALSE
142+
) {
143+
future_map2_vec(
144+
.x = .x,
145+
.y = vec_index(.x),
146+
.f = .f,
147+
...,
148+
.ptype = .ptype,
149+
.options = .options,
150+
.env_globals = .env_globals,
151+
.progress = .progress
152+
)
153+
}
154+
132155
#' @rdname future_imap
133156
#' @export
134157
future_imap_dfr <- function(

R/future-map.R

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
#' [progressr](https://CRAN.R-project.org/package=progressr)
5656
#' package.
5757
#'
58+
#' @param .ptype If `NULL`, the default, the output type is the common type of
59+
#' the elements of the result. Otherwise, supply a "prototype" giving the
60+
#' desired type of output.
61+
#'
5862
#' @return All functions return a vector the same length as `.x`.
5963
#'
6064
#' - [future_map()] returns a list
@@ -211,6 +215,34 @@ future_map_lgl <- function(
211215
)
212216
}
213217

218+
#' @rdname future_map
219+
#' @export
220+
future_map_vec <- function(
221+
.x,
222+
.f,
223+
...,
224+
.ptype = NULL,
225+
.options = furrr_options(),
226+
.env_globals = parent.frame(),
227+
.progress = FALSE
228+
) {
229+
out <- future_map(
230+
.x = .x,
231+
.f = .f,
232+
...,
233+
.options = .options,
234+
.env_globals = .env_globals,
235+
.progress = .progress
236+
)
237+
238+
simplify_impl(
239+
out,
240+
ptype = .ptype,
241+
error_arg = "<output>",
242+
error_call = current_env()
243+
)
244+
}
245+
214246
#' @rdname future_map
215247
#' @export
216248
future_map_dfr <- function(

R/future-map2.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,36 @@ future_map2_lgl <- function(
189189
)
190190
}
191191

192+
#' @rdname future_map2
193+
#' @export
194+
future_map2_vec <- function(
195+
.x,
196+
.y,
197+
.f,
198+
...,
199+
.ptype = NULL,
200+
.options = furrr_options(),
201+
.env_globals = parent.frame(),
202+
.progress = FALSE
203+
) {
204+
out <- future_map2(
205+
.x = .x,
206+
.y = .y,
207+
.f = .f,
208+
...,
209+
.options = .options,
210+
.env_globals = .env_globals,
211+
.progress = .progress
212+
)
213+
214+
simplify_impl(
215+
out,
216+
ptype = .ptype,
217+
error_arg = "<output>",
218+
error_call = current_env()
219+
)
220+
}
221+
192222
#' @rdname future_map2
193223
#' @export
194224
future_map2_dfr <- function(

R/future-pmap.R

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,34 @@ future_pmap_lgl <- function(
108108
)
109109
}
110110

111+
#' @rdname future_map2
112+
#' @export
113+
future_pmap_vec <- function(
114+
.l,
115+
.f,
116+
...,
117+
.ptype = NULL,
118+
.options = furrr_options(),
119+
.env_globals = parent.frame(),
120+
.progress = FALSE
121+
) {
122+
out <- future_pmap(
123+
.l = .l,
124+
.f = .f,
125+
...,
126+
.options = .options,
127+
.env_globals = .env_globals,
128+
.progress = .progress
129+
)
130+
131+
simplify_impl(
132+
out,
133+
ptype = .ptype,
134+
error_arg = "<output>",
135+
error_call = current_env()
136+
)
137+
}
138+
111139
#' @rdname future_map2
112140
#' @export
113141
future_pmap_dfr <- function(

R/utils-purrr.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,28 @@ at_selection <- function(nm, .at) {
4545
.at
4646
}
4747

48+
# `purrr:::simplify_impl()`, but simplified (ha!) by removing `strict`,
49+
# since we are always `strict`
50+
simplify_impl <- function(x, ptype, error_arg, error_call) {
51+
vctrs::obj_check_list(x, arg = error_arg, call = error_call)
52+
vctrs::list_check_all_vectors(x, arg = error_arg, call = error_call)
53+
vctrs::list_check_all_size(x, 1L, arg = error_arg, call = error_call)
54+
55+
names <- vctrs::vec_names(x)
56+
x <- vctrs::vec_set_names(x, NULL)
57+
58+
out <- vctrs::vec_c(
59+
!!!x,
60+
.ptype = ptype,
61+
.error_arg = error_arg,
62+
.error_call = error_call
63+
)
64+
65+
if (!is.null(out)) {
66+
out <- vctrs::vec_set_names(out, names)
67+
}
68+
69+
out
70+
}
71+
4872
# nocov end

man/future_imap.Rd

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/future_map.Rd

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/future_map2.Rd

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)