Anonymous user
Parametric polymorphism: Difference between revisions
→{{header|Julia}}
(Added C3 example) |
|||
Line 776:
=={{header|Julia}}==
{{works with|Julia|1.
<lang julia>
val::T▼
mutable struct BinaryTree{V}
lchild::Union{Tree{T}, Nothing}▼
rchild::Union{Tree{T}, Nothing}▼
end
map(f, bt::BinaryTree) = BinaryTree(f(bt.v), map(f, bt.l), map(f, bt.r))
map(f, bt::Nothing) = nothing
0,
BinaryTree(
),
BinaryTree(
),
)
end
let strtree = BinaryTree(
BinaryTree(
BinaryTree(
),
)
end
▲ inttree = Tree(0,
▲ Tree(1,
▲ Tree(3),
▲ Tree(5)),
▲ Tree(2,
▲ Tree(4),
▲ nothing))
▲ map!(x -> 2x^2, inttree) # Pass an anonymous function
▲ strtree = Tree("hello",
▲ Tree("world!",
▲ Tree("Julia"),
▲ nothing),
▲ Tree("foo",
▲ Tree("bar"),
▲ Tree("baz")))
▲ map!(uppercase, strtree)
end</lang>
|