10.12 Function factories + functionals
Combine functionals and function factories to turn data into many functions.
names <- list(
square = 2,
cube = 3,
root = 1/2,
cuberoot = 1/3,
reciprocal = -1
)
funs <- purrr::map(names, power1)
names(funs)
#> [1] "square" "cube" "root" "cuberoot" "reciprocal"
funs$root(64)
#> [1] 8
funs$square(3)
#> [1] 9
Avoid the prefix with
with()
-with(funs, root(100))
- Temporary, clear, short-term
attach()
-attach(funs)
/detach(funs)
- Added to search path (like package function), cannot be overwritten, but can be attached multiple times!
rlang::env_bind
-env_bind(globalenv(), !!!funs)
/env_unbind(gloablenv(), names(funs))
- Added to global env (like created function), can be overwritten