Parametric polymorphism: Difference between revisions

Content added Content deleted
m (Sorry I meant C# not C++!)
m (→‎{{header|REXX}}: added/changed comments and whitespace, changed separator.)
Line 870: Line 870:
=={{header|REXX}}==
=={{header|REXX}}==
This REXX programming example is modeled after the   '''D'''   example.
This REXX programming example is modeled after the   '''D'''   example.
<lang rexx>/*REXX program demonstrates a method of parametric polymorphism in REXX.*/
<lang rexx>/*REXX program demonstrates (with displays) a method of parametric polymorphism in REXX.*/
call newRoot 1.00, 3 /*new root and indicate 3 stems. */
call newRoot 1.00, 3 /*new root, and also indicate 3 stems.*/
/* [↓] no need to label the stems*/
/* [↓] no need to label the stems. */
call addStem 1.10 /*new stem and its initial value.*/
call addStem 1.10 /*a new stem and its initial value. */
call addStem 1.11 /* " " " " " " */
call addStem 1.11 /*" " " " " " " */
call addStem 1.12 /* " " " " " " */
call addStem 1.12 /*" " " " " " " */
call addStem 1.20 /* " " " " " " */
call addStem 1.20 /*" " " " " " " */
call addStem 1.21 /* " " " " " " */
call addStem 1.21 /*" " " " " " " */
call addStem 1.22 /* " " " " " " */
call addStem 1.22 /*" " " " " " " */
call sayNodes /*display nicely formatted values*/
call sayNodes /*display some nicely formatted values.*/
call modRoot 50 /*MOD will add 50 to all stems. */
call modRoot 50 /*modRoot will add fifty to all stems. */
call sayNodes /*display nicely formatted values*/
call sayNodes /*display some nicely formatted values.*/
exit /*stick a fork in it, we're done.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────MODROOT─────────────────────────────*/
modRoot: do j=1 for nodes /*traipse through all the nodes. */
addStem: nodes=nodes+1; do j=1 for stems; root.nodes.j=arg(1); end; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
do k=1 for stems; _=root.j.k; ?=datatype(_,'N')
if ? then root.j.k=_+arg(1) /*can stem be added to?*/
modRoot: do j=1 for nodes /*traipse through all the defined nodes*/
end /*k*/ /* [↑] only add if it's numeric.*/
do k=1 for stems
end /*j*/
if datatype(root.j.k, 'N') then root.j.k=root.j.k + arg(1) /*bias.*/
end /*k*/ /* [↑] only add if numeric stem value.*/
return
end /*j*/
/*──────────────────────────────────NEWROOT─────────────────────────────*/
return
newRoot: stems=arg(2); nodes=-1; /*set NODES to a kind of "null". */
/*──────────────────────────────────────────────────────────────────────────────────────*/
call addStem copies('',9); call addStem arg(1)
newRoot: stems=arg(2); nodes= -1 /*set NODES to a kind of "null". */
return
call addStem copies('', 9); call addStem arg(1)
/*──────────────────────────────────SAYNODES────────────────────────────*/
return
sayNodes: say; do j=0 to nodes; _= /*each node gets shown*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
do k=1 for stems; _=_ right(root.j.k,9); end /*k*/
say substr(_,2) /*ignore the 1st blank*/
sayNodes: say; do j=0 to nodes; _= /*ensure each of the nodes gets shown. */
do k=1 for stems; _=_ right(root.j.k, 9)
end /*k*/
say substr(_, 2) /*ignore the first (leading) blank. */
end /*j*/
end /*j*/


say left('', stems*9+stems) || '('nodes" nodes)" /*also show # of nodes*/
say left('', stems*11) || '('nodes" nodes)" /*also show number of nodes.*/
return
return</lang>
{{out|output|text=&nbsp; when using the default input:}}
/*──────────────────────────────────ADDSTEM─────────────────────────────*/
addStem: nodes=nodes+1; do j=1 for stems; root.nodes.j=arg(1); end /*j*/
return</lang>
'''output'''
<pre>
<pre>
═════════ ═════════ ═════════
───────── ───────── ─────────
1.00 1.00 1.00
1.00 1.00 1.00
1.10 1.10 1.10
1.10 1.10 1.10
Line 915: Line 915:
1.21 1.21 1.21
1.21 1.21 1.21
1.22 1.22 1.22
1.22 1.22 1.22
(7 nodes)
(7 nodes)


═════════ ═════════ ═════════
───────── ───────── ─────────
51.00 51.00 51.00
51.00 51.00 51.00
51.10 51.10 51.10
51.10 51.10 51.10
Line 925: Line 925:
51.21 51.21 51.21
51.21 51.21 51.21
51.22 51.22 51.22
51.22 51.22 51.22
(7 nodes)
(7 nodes)
</pre>
</pre>