Flat map is similar to map, except that the size restriction on the output
of each .f
call is lifted. This means that rather than requiring a result
of size 1
from each function call, a flat map can return an object of
arbitrary size.
flat_map_vec(.x, .f, ..., .ptype = NULL, .name_spec = NULL, .name_repair = c("minimal", "unique", "check_unique", "universal")) flat_map_lst(.x, .f, ..., .name_spec = NULL, .name_repair = c("minimal", "unique", "check_unique", "universal")) flat_map_dbl(.x, .f, ..., .name_spec = NULL, .name_repair = c("minimal", "unique", "check_unique", "universal")) flat_map_int(.x, .f, ..., .name_spec = NULL, .name_repair = c("minimal", "unique", "check_unique", "universal")) flat_map_chr(.x, .f, ..., .name_spec = NULL, .name_repair = c("minimal", "unique", "check_unique", "universal")) flat_map_lgl(.x, .f, ..., .name_spec = NULL, .name_repair = c("minimal", "unique", "check_unique", "universal"))
.x | A list or atomic vector. |
---|---|
.f | A function, formula, or vector (not necessarily atomic). If a function, it is used as is. If a formula, e.g.
This syntax allows you to create very compact anonymous functions. If character vector, numeric vector, or list, it is
converted to an extractor function. Character vectors index by
name and numeric vectors index by position; use a list to index
by position and name at different levels. If a component is not
present, the value of |
... | Vectors to coerce. |
.ptype | If Alternatively, you can supply |
.name_spec | A name specification for combining
inner and outer names. This is relevant for inputs passed with a
name, when these inputs are themselves named, like
See the name specification topic. |
.name_repair | How to repair names, see |
vec_size(flat_map_vec(.x, .f)) == sum(map_int(map(.x, .f), vec_size))
vec_ptype(flat_map_vec(.x, .ptype = ptype)) == ptype %||% vec_ptype_common(!!! map(.x, .f))
# `map()` functions require that each result from `.f` have size 1 try(map_dbl(list(1, 2:3, 4:6), ~.x))#> Error in map_dbl(list(1, 2:3, 4:6), ~.x) : #> could not find function "map_dbl"#> [1] 1 2 3 4 5 6#> [1] "2019-10-24" "2019-10-25" "2019-10-24" "2019-10-25" "2019-10-26" #> [6] "2019-10-24" "2019-10-25" "2019-10-26" "2019-10-27"#> x #> 1 1 #> 2 1 #> 3 2 #> 4 1 #> 5 2 #> 6 3# Name repair if required flat_map_dbl(list(x = 1, x = 2), ~c(a = .x), .name_spec = "{outer}_{inner}")#> x_a x_a #> 1 2