Nested templated data: Difference between revisions

Added Bracmat solution
(Added Bracmat solution)
Line 36:
 
''Show output on this page.''
=={{header|Bracmat}}==
The uninstantiated template and the instantiated template are JSON structures. The payloads are listed in a Bracmat list. The <code>get</code> function is instructed to read input from memory and to parse it as JSON. The output of this call is a Bracmat structure (not shown) that is assigned to <code>template</code>. The <code>instantiate</code> function recursively traverses the template's tree structure. If the current node is a number, then that number is used as the key into the payloads list. The corresponding payload is then returned. If the current node is not a number, then it is assumed that the current node is a binary (sub)tree. The left hand side (called <code>a</code>) and the right hand side (called <code>b</code>) are instantiated and their combination is returned. Finally the instantiated tree is transformed back to JSON format and output.
<lang bracmat>( get$("[
[[1, 2],
[3, 4, 1],
5]]",MEM,JSN)
: ?template
& (0.payload#0)
(1.payload#1)
(2.payload#2)
(3.payload#3)
(4.payload#4)
(5.payload#5)
(6.payload#6)
: ?payloads
& ( instantiate
= tmplt plds pld a b
. !arg:(?tmplt.?plds)
& ( !tmplt:#
& !plds:? (!tmplt.?pld) ?
& !pld
| !tmplt:%?a_%?b
& (instantiate$(!a.!plds))_(instantiate$(!b.!plds))
)
)
& out$(jsn$(instantiate$(!template.!payloads)))
);</lang>
{{out}}
<pre>[[[payload#1,payload#2],[payload#3,payload#4,payload#1],payload#5]]</pre>
 
=={{header|Factor}}==
483

edits