June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,5 +1,11 @@
[[wp:Parametric Polymorphism|Parametric Polymorphism]] is a way to define types or functions that are generic over other types. The genericity can be expressed by using ''type variables'' for the parameter type, and by a mechanism to explicitly or implicitly replace the type variables with concrete types when necessary.
Write a small example for a type declaration that is parametric over another type, together with a short bit of code (and its type signature) that uses it. A good example is a container type, let's say a binary tree, together with some function that traverses the tree, say, a ''map''-function that operates on every element of the tree.
;Task:
Write a small example for a type declaration that is parametric over another type, together with a short bit of code (and its type signature) that uses it.
A good example is a container type, let's say a binary tree, together with some function that traverses the tree, say, a ''map''-function that operates on every element of the tree.
This language feature only applies to statically-typed languages.
<br><br>

View file

@ -0,0 +1,12 @@
mutable struct Tree{T}
value::T
lchild::Nullable{Tree{T}}
rchild::Nullable{Tree{T}}
end
function replaceall!(t::Tree{T}, v::T) where T
t.value = v
isnull(lchild) || replaceall(get(lchild), v)
isnull(rchild) || replaceall(get(rchild), v)
return t
end

View file

@ -1,4 +1,4 @@
/*REXX program demonstrates (with displays) a method of parametric polymorphism in REXX.*/
/*REXX program demonstrates (with displays) a method of parametric polymorphism. */
call newRoot 1.00, 3 /*new root, and also indicate 3 stems.*/
/* [↓] no need to label the stems. */
call addStem 1.10 /*a new stem and its initial value. */
@ -12,24 +12,21 @@ call modRoot 50 /*modRoot will add fifty to all
call sayNodes /*display some nicely formatted values.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
addStem: nodes=nodes+1; do j=1 for stems; root.nodes.j=arg(1); end; return
addStem: nodes=nodes + 1; do j=1 for stems; root.nodes.j=arg(1); end; return
newRoot: parse arg @,stems; nodes=-1; call addStem copies('',9); call addStem @; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
modRoot: do j=1 for nodes /*traipse through all the defined nodes*/
do k=1 for stems
if datatype(root.j.k, 'N') then root.j.k=root.j.k + arg(1) /*bias.*/
end /*k*/ /* [↑] only add if numeric stem value.*/
end /*j*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
newRoot: stems=arg(2); nodes= -1 /*set NODES to a kind of "null". */
call addStem copies('', 9); call addStem arg(1)
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
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. */
modRoot: arg #; do j=1 for nodes /*traipse through all the defined nodes*/
do k=1 for stems
if datatype(root.j.k,'N') then root.j.k=root.j.k + # /*add bias.*/
end /*k*/ /* [↑] only add if numeric stem value.*/
end /*j*/
say left('', stems*11) || '('nodes" nodes)" /*also show number of nodes.*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
sayNodes: w=9; do j=0 to nodes; _= /*ensure each of the nodes gets shown. */
do k=1 for stems; _=_ center(root.j.k, w) /*concatenate a node*/
end /*k*/
$=word('node='j, 1 + (j<1) ) /*define a label for this line's output*/
say center($, w) substr(_, 2) /*ignore 1st (leading) blank which was */
end /*j*/ /* [↑] caused by concatenation.*/
say /*show a blank line to separate outputs*/
return /* [↑] extreme indentation to terminal*/