Parametric polymorphism: Difference between revisions

Content added Content deleted
m (→‎{{header|Raku}}: .perl not needed)
(Added a procedure "initTree", a procedure "map", a procedure "print" and an example.)
Line 834: Line 834:


=={{header|Nim}}==
=={{header|Nim}}==
<lang nim>type Tree[T] = ref object
<lang nim>import strutils, sugar

type Tree[T] = ref object
value: T
value: T
left, right: Tree[T]</lang>
left, right: Tree[T]


proc newTree[T](value = default(T)): Tree[T] =
## Create a tree with a single node with the given value.
Tree[T](value: value)


proc map[T, U](tree: Tree[T]; f: (T) -> U): Tree[U] =
## Apply function "f" to each element of a tree, building
## another tree.
result = newTree[U](f(tree.value))
if not tree.left.isNil:
result.left = tree.left.map(f)
if not tree.right.isNil:
result.right = tree.right.map(f)


proc print(tree: Tree; indent = 0) =
## Print a tree.
let start = repeat(' ', indent)
echo start, "value: ", tree.value
if tree.left.isNil:
echo start, " nil"
else:
print(tree.left, indent + 2)
if tree.right.isNil:
echo start, " nil"
else:
print(tree.right, indent + 2)


when isMainModule:

echo "Initial tree:"
var tree = newTree[int](5)
tree.left = newTree[int](2)
tree.right = newTree[int](7)
print(tree)

echo ""
echo "Tree created by applying a function to each node:"
let tree1 = tree.map((x) => 1 / x)
print(tree1)</lang>

{{out}}
<pre>Initial tree:
value: 5
value: 2
nil
nil
value: 7
nil
nil

Tree created by applying a function to each node:
value: 0.2
value: 0.5
nil
nil
value: 0.1428571428571428
nil
nil</pre>


=={{header|Objective-C}}==
=={{header|Objective-C}}==