Parametric polymorphism: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 93:
 
Arguably more interesting is run time polymorphism, which can't be trivially done; if you are confident in your coding skill, you could keep track of data types and method dispatch at run time yourself -- but then, you are probably too confident to not realize you might be better off using some higher level languages.
 
=={{header|C++}}==
 
<lang cpp>template<class T>
class tree
{
T value;
tree *left;
tree *right;
public:
void replace_all (T new_value);
};</lang>
 
For simplicity, we replace all values in the tree with a new value:
 
<lang cpp>template<class T>
void tree<T>::replace_all (T new_value)
{
value = new_value;
if (left != NULL)
left->replace_all (new_value);
if (right != NULL)
right->replace_all (new_value);
}</lang>
 
=={{header|C sharp|C#}}==
Line 220 ⟶ 196:
2.5
3.5</pre>
 
=={{header|C++}}==
 
<lang cpp>template<class T>
class tree
{
T value;
tree *left;
tree *right;
public:
void replace_all (T new_value);
};</lang>
 
For simplicity, we replace all values in the tree with a new value:
 
<lang cpp>template<class T>
void tree<T>::replace_all (T new_value)
{
value = new_value;
if (left != NULL)
left->replace_all (new_value);
if (right != NULL)
right->replace_all (new_value);
}</lang>
 
=={{header|Ceylon}}==
Line 467:
? t :Tree[int]
# value: <tree></lang>
 
 
=={{header|F_Sharp|F#}}==
Line 488 ⟶ 487:
let t2 = t1.Map(fun x -> x * 10)
</lang>
 
 
=={{header|Fortran}}==
Line 682 ⟶ 680:
If we have a tree of integers, i.e. <var>f</var> is <code>Tree</code> and <var>a</var> is <code>Integer</code>, then the type of <code>add1Everywhere</code> is <code>Tree Integer -> Tree Integer</code>.
</small></blockquote>
 
=={{header|Icon}} and {{header|Unicon}}==
 
Like PicoLisp, Icon and Unicon are dynamically typed and hence inherently polymorphic.
Here's an example that can apply a function to the nodes in an <i>n</i>-tree regardless of
the type of each node. It is up to the function to decide what to do with a given type
of node. Note that the nodes do no even have to be of the same type.
 
<lang unicon>procedure main()
bTree := [1, [2, [4, [7]], [5]], [3, [6, [8], [9]]]]
mapTree(bTree, write)
bTree := [1, ["two", ["four", [7]], [5]], [3, ["six", ["eight"], [9]]]]
mapTree(bTree, write)
end
 
procedure mapTree(tree, f)
every f(\tree[1]) | mapTree(!tree[2:0], f)
end</lang>
 
=={{header|Inform 7}}==
Line 708 ⟶ 724:
text valued table column
phrase (text, text) -> number</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
 
Like PicoLisp, Icon and Unicon are dynamically typed and hence inherently polymorphic.
Here's an example that can apply a function to the nodes in an <i>n</i>-tree regardless of
the type of each node. It is up to the function to decide what to do with a given type
of node. Note that the nodes do no even have to be of the same type.
 
<lang unicon>procedure main()
bTree := [1, [2, [4, [7]], [5]], [3, [6, [8], [9]]]]
mapTree(bTree, write)
bTree := [1, ["two", ["four", [7]], [5]], [3, ["six", ["eight"], [9]]]]
mapTree(bTree, write)
end
 
procedure mapTree(tree, f)
every f(\tree[1]) | mapTree(!tree[2:0], f)
end</lang>
 
=={{header|J}}==
Line 845 ⟶ 843:
 
{{omit from|Oforth|Oforth is nt statically-typed language}}
 
=={{header|Perl 6}}==
<lang perl6>role BinaryTree[::T] {
has T $.value;
has BinaryTree[T] $.left;
has BinaryTree[T] $.right;
 
method replace-all(T $value) {
$!value = $value;
$!left.replace-all($value) if $!left.defined;
$!right.replace-all($value) if $!right.defined;
}
}
 
class IntTree does BinaryTree[Int] { }
 
my IntTree $it .= new(value => 1,
left => IntTree.new(value => 2),
right => IntTree.new(value => 3));
 
$it.replace-all(42);
say $it.perl;</lang>
{{out}}
<pre>IntTree.new(value => 42, left => IntTree.new(value => 42, left => BinaryTree[T], right => BinaryTree[T]), right => IntTree.new(value => 42, left => BinaryTree[T], right => BinaryTree[T]))</pre>
 
=={{header|Phix}}==
Line 1,045 ⟶ 1,019:
(Node 6 (Node 4 #f #f) #f))
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>role BinaryTree[::T] {
has T $.value;
has BinaryTree[T] $.left;
has BinaryTree[T] $.right;
 
method replace-all(T $value) {
$!value = $value;
$!left.replace-all($value) if $!left.defined;
$!right.replace-all($value) if $!right.defined;
}
}
 
class IntTree does BinaryTree[Int] { }
 
my IntTree $it .= new(value => 1,
left => IntTree.new(value => 2),
right => IntTree.new(value => 3));
 
$it.replace-all(42);
say $it.perl;</lang>
{{out}}
<pre>IntTree.new(value => 42, left => IntTree.new(value => 42, left => BinaryTree[T], right => BinaryTree[T]), right => IntTree.new(value => 42, left => BinaryTree[T], right => BinaryTree[T]))</pre>
 
=={{header|REXX}}==
10,327

edits