I think it's more accurate to think of it the other way around i.e. map() applies the function in .f to each element of .x.
# mean() is applied to each vector in the list.
purrr::map(.x = list(c(1:3), c(4:6)), .f = mean)
#> [[1]]
#> [1] 2
#>
#> [[2]]
#> [1] 5
Created on 2020-05-22 by the reprex package (v0.3.0)
However, for the problem at hand, we want to test whether each element meets a specified condition. In essence, we want to use the element as an argument in a function call. In order to do that, we need to define an anonymous function.
purrr simply provides ~ as a shorthand to save typing. For example, the two calls below are equivalent.
library(tidyverse)
testlist <- list(
list(aaa_x = 1, aaa_y = 2, aaa_z = 5, bbb_a = 333, bbb_b = 222),
list(aaa_x = 7, aaa_y = 5, aaa_z = 6, bbb_a = 3939, bbb_b = 5635)
)
result_1 <- map(testlist, function(x) keep(x, .p = str_detect(names(x), "aaa")))
result_2 <- map(testlist, ~ keep(.x, .p = str_detect(names(.x), "aaa")))
identical(result_1, result_2)
#> [1] TRUE
Created on 2020-05-22 by the reprex package (v0.3.0)