Anonymous user
Parametric polymorphism: Difference between revisions
m
→{{header|Julia}}
Line 758:
end
Tree(v::T) where
"""
new types should override the `show()` function for pretty-printing.
"""
function map!(f::Function, bt::Tree{T}) where T
bt.val
print(" (" * string(bt.val)) #
bt.lchild !== nothing && map!(f, bt.lchild)
bt.rchild !== nothing && map!(f, bt.rchild)
print(")") #
end
function test_treemap()
Tree(1,
Tree(3),
Tree(5)),
Tree(2,
Tree(4),
map!(x -> 2x^2,
#
println()
strtree = Tree("hello",
Tree("world!",
Tree("Julia"),
nothing),
Tree("foo",
Tree("bar"),
Tree("baz")))
map!(uppercase, strtree)
# (HELLO (WORLD! (JULIA)) (FOO (BAR) (BAZ)))
end</lang>
|