Funkcyjne programowanie średnio zaawansowane z purrr
Colin Fay
Data Scientist & R Hacker at ThinkR
Funkcja:
Liczba $n$:
Wektor znakowy $z$
Gdy .f jest funkcją, może być:
my_fun <- function(x) {
round(mean(x))
}
map_dbl(visit_2014, my_fun)
[1] 5526 6546 6097 7760
[5] 7025 7162 10484 8256
[9] 6558 7686 5723 5053
map_dbl(visit_2014, function(x) {
round(mean(x))
})
[1] 5526 6546 6097 7760
[5] 7025 7162 10484 8256
[9] 6558 7686 5723 5053
mapper: funkcja anonimowa z formułą jednostronną
# Z jednym parametrem
map_dbl(visits2017, ~ round(mean(.x)))
# Równoważne z
map_dbl(visits2017, ~ round(mean(.)))
# Równoważne z
map_dbl(visits2017, ~ round(mean(..1)))
mapper: funkcja anonimowa z formułą jednostronną
# Z dwoma parametrami
map2(visits2016, visits2017, ~ .x + .y)
# Równoważne z
map2(visits2016, visits2017, ~ ..1 + ..2)
# Z więcej niż dwoma parametrami
pmap(list, ~ ..1 + ..2 + ..3)
as_mapper(): tworzenie obiektów mapper z funkcji lambda
# Klasyczna funkcja
round_mean <- function(x){
round(mean(x))
}
# Jako mapper
round_mean <- as_mapper(~ round(mean(.x))))
Mappery są:
Zwięzłe
Czytelne
Wielokrotnego użytku

Funkcyjne programowanie średnio zaawansowane z purrr