In R, I\'m trying to create a way to transform function parameters given in ...
to values in a pre-determined list within a closure function.
I would like t
Post-order depth first walk of nested list
postwalk<-function(x,f) {
if(is.list(x)) f(lapply(x,postwalk,f)) else f(x)
}
Replacement function that returns modified list rather than mutating in place
replace.kv<-function(x,m) {
if(!is.list(x)) return(x)
i<-match(names(x),names(m));
w<-which(!is.na(i));
replace(x,w,m[i[w]])
}
Example
t<-list(a="a1", b="b1", c=list(d="d1", e="e1"))
s<-list(a="a2", d="d2")
str(postwalk(t,function(x) replace.kv(x,s)))
List of 3 $ a: chr "a2" $ b: chr "b1" $ c:List of 2 ..$ d: chr "d2" ..$ e: chr "e1"