🐌 I want to …

create three normal distribution with purrr::map as simple as possible

🐌 Here’s how to:

library(tidyverse)
#rnorm(n=5, mean = mu, sd = sigma) #函数的一般形式

black_box <- partial(rnorm, n=5)# 先把不变的固定下来,生成新的黑箱函数,黑箱函数只需要接受 map 传递的参数即可, 其他的参数已经通过 ·partial· 固定下来了

mu <- list(10, 100, -100)# 参数 x
sigma <- list(0.01, 1, 10)# 参数 y

map2(mu, sigma, ~ black_box(mean=.x, sd=.y)) %>% 
  do.call(cbind, .)
##           [,1]      [,2]       [,3]
## [1,] 10.019420  99.98588  -94.94470
## [2,]  9.988821  99.71190  -83.55759
## [3,]  9.998299 100.89318 -104.27379
## [4,]  9.982813  99.90448  -95.00314
## [5,]  9.995378  99.57725 -115.84197

🐌 Ok, but why?

这个方法特别适合用于拥有复杂参数的函数 , 比如机器学习的算法中,eg:线性回归