I'm trying to write a function which can take in functions as its arguments in Rcpp. I have written an example function in R that shows the kind of functionality that I'm aiming for:
simulate_and_evaluate <- function(simulate, evaluate) {
y <- simulate(1)
eval <- evaluate(y)
return(eval)
}
simulate_fun <- function(n) rnorm(n, 0, 1)
evaluate_fun <- function(x) dnorm(x, 0, 1)
simulate_and_evaluate(simulate = simulate_fun,
evaluate = evaluate_fun)
In this function simulate_and_evaluate
, this takes in two arguments which are both functions, one that simulates a number and one that evaluates a function with this simualted number. So as an example, we can simulate a value from a standard normal and evaluate the density of a standard normal at that point. Does anyone know if there's a way to do this in Rcpp?