Nested templated data: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: Fix comment: Perl 6 --> Raku) |
|||
Line 183: | Line 183: | ||
</pre> |
</pre> |
||
=={{header|J}}== |
|||
<pre> |
|||
NB. algorithm: evaluate after rewrite as a j sentence |
|||
NB. because arrays are homogeneous in data type |
|||
NB. 1) data type validity for an operation takes place just once for all the values in the array which is fast |
|||
NB. 2) but a box next to a 5 is invalid. |
|||
NB. hence the payloads must be individually boxed as well. |
|||
] p =: ('(<''payload#',''')',~":)&>i.7 |
|||
(<'payload#0') |
|||
(<'payload#1') |
|||
(<'payload#2') |
|||
(<'payload#3') |
|||
(<'payload#4') |
|||
(<'payload#5') |
|||
(<'payload#6') |
|||
Substitute=: ({.@:[)`(I.@:(= {:)~)`]} |
|||
NB. aside: demonstrate Substitution |
|||
] A =: ;/ i. 8 |
|||
┌─┬─┬─┬─┬─┬─┬─┬─┐ |
|||
│0│1│2│3│4│5│6│7│ |
|||
└─┴─┴─┴─┴─┴─┴─┴─┘ |
|||
] B =: ('xxxx' ; 3) |
|||
┌────┬─┐ |
|||
│xxxx│3│ |
|||
└────┴─┘ |
|||
B Substitute A |
|||
┌─┬─┬─┬────┬─┬─┬─┬─┐ |
|||
│0│1│2│xxxx│4│5│6│7│ |
|||
└─┴─┴─┴────┴─┴─┴─┴─┘ |
|||
B Substitute A , < 3 |
|||
┌─┬─┬─┬────┬─┬─┬─┬─┬────┐ |
|||
│0│1│2│xxxx│4│5│6│7│xxxx│ |
|||
└─┴─┴─┴────┴─┴─┴─┴─┴────┘ |
|||
NB. return to task |
|||
NB. though they look scalar the words are vector |
|||
] t =: ;: CRLF -.~ 0 :0 |
|||
[ |
|||
[[1, 2], |
|||
[3, 4, 1], |
|||
5]] |
|||
) |
|||
┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐ |
|||
│[│[│[│1│,│2│]│,│[│3│,│4│,│1│]│,│5│]│]│ |
|||
└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘ |
|||
t =: ,: t NB. itemize t, then append successive substitutions |
|||
t =: t , ('(<' ; , '[') Substitute {: t |
|||
t =: t , ( ')' ; , ']') Substitute {: t |
|||
t |
|||
┌──┬──┬──┬─┬─┬─┬─┬─┬──┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐ |
|||
│[ │[ │[ │1│,│2│]│,│[ │3│,│4│,│1│]│,│5│]│]│ |
|||
├──┼──┼──┼─┼─┼─┼─┼─┼──┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ |
|||
│(<│(<│(<│1│,│2│]│,│(<│3│,│4│,│1│]│,│5│]│]│ |
|||
├──┼──┼──┼─┼─┼─┼─┼─┼──┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ |
|||
│(<│(<│(<│1│,│2│)│,│(<│3│,│4│,│1│)│,│5│)│)│ |
|||
└──┴──┴──┴─┴─┴─┴─┴─┴──┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘ |
|||
NB. finish the substitutions in one fell swoop |
|||
[ t =: > (] , (Substitute {:))&.>/(<"1 SUBSTITUTIONS) , < t |
|||
┌──┬──┬──┬──────────────┬─┬──────────────┬─┬─┬──┬──────────────┬─┬──────────────┬─┬──────────────┬─┬─┬──────────────┬─┬─┐ |
|||
│[ │[ │[ │1 │,│2 │]│,│[ │3 │,│4 │,│1 │]│,│5 │]│]│ |
|||
├──┼──┼──┼──────────────┼─┼──────────────┼─┼─┼──┼──────────────┼─┼──────────────┼─┼──────────────┼─┼─┼──────────────┼─┼─┤ |
|||
│(<│(<│(<│1 │,│2 │]│,│(<│3 │,│4 │,│1 │]│,│5 │]│]│ |
|||
├──┼──┼──┼──────────────┼─┼──────────────┼─┼─┼──┼──────────────┼─┼──────────────┼─┼──────────────┼─┼─┼──────────────┼─┼─┤ |
|||
│(<│(<│(<│1 │,│2 │)│,│(<│3 │,│4 │,│1 │)│,│5 │)│)│ |
|||
├──┼──┼──┼──────────────┼─┼──────────────┼─┼─┼──┼──────────────┼─┼──────────────┼─┼──────────────┼─┼─┼──────────────┼─┼─┤ |
|||
│(<│(<│(<│1 │,│2 │)│,│(<│3 │,│4 │,│1 │)│,│5 │)│)│ |
|||
├──┼──┼──┼──────────────┼─┼──────────────┼─┼─┼──┼──────────────┼─┼──────────────┼─┼──────────────┼─┼─┼──────────────┼─┼─┤ |
|||
│(<│(<│(<│1 │,│2 │)│,│(<│3 │,│4 │,│1 │)│,│(<'payload#5')│)│)│ |
|||
├──┼──┼──┼──────────────┼─┼──────────────┼─┼─┼──┼──────────────┼─┼──────────────┼─┼──────────────┼─┼─┼──────────────┼─┼─┤ |
|||
│(<│(<│(<│1 │,│2 │)│,│(<│3 │,│(<'payload#4')│,│1 │)│,│(<'payload#5')│)│)│ |
|||
├──┼──┼──┼──────────────┼─┼──────────────┼─┼─┼──┼──────────────┼─┼──────────────┼─┼──────────────┼─┼─┼──────────────┼─┼─┤ |
|||
│(<│(<│(<│1 │,│2 │)│,│(<│(<'payload#3')│,│(<'payload#4')│,│1 │)│,│(<'payload#5')│)│)│ |
|||
├──┼──┼──┼──────────────┼─┼──────────────┼─┼─┼──┼──────────────┼─┼──────────────┼─┼──────────────┼─┼─┼──────────────┼─┼─┤ |
|||
│(<│(<│(<│1 │,│(<'payload#2')│)│,│(<│(<'payload#3')│,│(<'payload#4')│,│1 │)│,│(<'payload#5')│)│)│ |
|||
├──┼──┼──┼──────────────┼─┼──────────────┼─┼─┼──┼──────────────┼─┼──────────────┼─┼──────────────┼─┼─┼──────────────┼─┼─┤ |
|||
│(<│(<│(<│(<'payload#1')│,│(<'payload#2')│)│,│(<│(<'payload#3')│,│(<'payload#4')│,│(<'payload#1')│)│,│(<'payload#5')│)│)│ |
|||
├──┼──┼──┼──────────────┼─┼──────────────┼─┼─┼──┼──────────────┼─┼──────────────┼─┼──────────────┼─┼─┼──────────────┼─┼─┤ |
|||
│(<│(<│(<│(<'payload#1')│,│(<'payload#2')│)│,│(<│(<'payload#3')│,│(<'payload#4')│,│(<'payload#1')│)│,│(<'payload#5')│)│)│ |
|||
└──┴──┴──┴──────────────┴─┴──────────────┴─┴─┴──┴──────────────┴─┴──────────────┴─┴──────────────┴─┴─┴──────────────┴─┴─┘ |
|||
". ;:inv {: t NB. task output |
|||
┌───────────────────────────────────────────────────────────────────┐ |
|||
│┌─────────────────────────────────────────────────────────────────┐│ |
|||
││┌─────────────────────┬───────────────────────────────┬─────────┐││ |
|||
│││┌─────────┬─────────┐│┌─────────┬─────────┬─────────┐│payload#5│││ |
|||
││││payload#1│payload#2│││payload#3│payload#4│payload#1││ │││ |
|||
│││└─────────┴─────────┘│└─────────┴─────────┴─────────┘│ │││ |
|||
││└─────────────────────┴───────────────────────────────┴─────────┘││ |
|||
│└─────────────────────────────────────────────────────────────────┘│ |
|||
└───────────────────────────────────────────────────────────────────┘ |
|||
NB. finally, look at the representation of the substitution gerund |
|||
({.@:[)`(I.@:(= {:)~)`] |
|||
┌───────────┬──────────────────────────┬─┐ |
|||
│┌──┬──────┐│┌─┬──────────────────────┐│]│ |
|||
││@:│┌──┬─┐│││~│┌────────────────────┐││ │ |
|||
││ ││{.│[││││ ││┌──┬───────────────┐│││ │ |
|||
││ │└──┴─┘│││ │││@:│┌──┬──────────┐││││ │ |
|||
│└──┴──────┘││ │││ ││I.│┌─┬──────┐│││││ │ |
|||
│ ││ │││ ││ ││2│┌─┬──┐││││││ │ |
|||
│ ││ │││ ││ ││ ││=│{:│││││││ │ |
|||
│ ││ │││ ││ ││ │└─┴──┘││││││ │ |
|||
│ ││ │││ ││ │└─┴──────┘│││││ │ |
|||
│ ││ │││ │└──┴──────────┘││││ │ |
|||
│ ││ ││└──┴───────────────┘│││ │ |
|||
│ ││ │└────────────────────┘││ │ |
|||
│ │└─┴──────────────────────┘│ │ |
|||
└───────────┴──────────────────────────┴─┘ |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
The array structure needs to be specified as Any type of data, to allow assignment of String to positions in the array originally containing integers. |
The array structure needs to be specified as Any type of data, to allow assignment of String to positions in the array originally containing integers. |