recursion - Julia - equivalent of recursive sapply function in R -
i had function in r (onestep
below) took argument vector v
, returned new vector v
output function of input vector. iterated function niter
times , kept output vectors of each iteration (which not same length , can end having length 0) in function iterate
follows (minimal example) :
onestep = function (v) c(v,2*v) iterate = function (v, niter) sapply(1:niter, function (iter) {v <<- onestep(v) return(v) } )
example :
v=c(1,2,3) iterate(v,3) [[1]] [1] 1 2 3 2 4 6 [[2]] [1] 1 2 3 2 4 6 2 4 6 4 8 12 [[3]] [1] 1 2 3 2 4 6 2 4 6 4 8 12 2 4 6 4 8 12 4 8 12 8 16 24
i wondering compact , idiomatic way such recursive function returns intermediate results in julia? thoughts? (apologies if trivial new julia)
not sure on compact , idiomatic front, how i'd it
onestep(v) = [v 2*v] function iterate(v, niter) results = array(array, niter) results[1] = onestep(v) idx = 2:niter results[idx] = onestep(results[idx - 1]) end results end v = [1 2 3] iterate(v, 3)
Comments
Post a Comment