Nested templated data: Difference between revisions

add implementation in R
(Added C++ implementation)
(add implementation in R)
Line 894:
MISSING PAYLOADS:
??#10</pre>
 
=={{header|R}}==
 
This is a fairly straightforward implementation in R using a recursive function. It's not likely the most efficient in the world.
 
<lang R>fill_template <- function(x, template, prefix = "Payload#") {
for (i in seq_along(template)) {
temp_slice <- template[[i]]
if (is.list(temp_slice)) {
template[[i]] <- fill_template(x, temp_slice, prefix)
} else {
temp_slice <- paste0(prefix, temp_slice)
template[[i]] <- x[match(temp_slice, x)]
}
}
return(template)
}
 
library("jsonlite") # for printing the template and result
template <- list(list(c(1, 2), c(3, 4), 5))
payload <- paste0("Payload#", 0:6)
result <- fill_template(payload, template)
 
cat(sprintf(
"Template\t%s\nPayload\t%s\nResult\t%s",
toJSON(template, auto_unbox = TRUE),
toJSON(payload, auto_unbox = TRUE),
toJSON(result, auto_unbox = TRUE)
))</lang>
 
{{out}}
<pre>Template [[[1,2],[3,4],5]]
Payload ["Payload#0","Payload#1","Payload#2","Payload#3","Payload#4","Payload#5","Payload#6"]
Result [[["Payload#1","Payload#2"],["Payload#3","Payload#4"],"Payload#5"]]</pre>
 
 
=={{header|Racket}}==
Anonymous user