November 2017
Beginner
316 pages
6h 40m
English
To understand why metaprogramming is used, have a look at this scenario. You have a function similar to the one given here, which takes a number and prints it the given number of times:
julia> function foo(n::Int64)
for i=1:n
println("foo")
end
end
foo (generic function with 1 method)
julia> foo(2)
foo
foo
Simple? Yes. But now suppose, in some different module in your code, you are doing the same kind of operation, but with some other name:
julia> function bar(n::Int64)
for i=1:n
println("bar")
end
end
bar (generic function with 1 method)
julia> bar(2)
bar
bar
julia> function baz(n::Int64)
for i=1:n
println("baz")
end
end
baz (generic function with 1 method)
julia> baz(2)
baz
baz
A lot of code repetition, right? You ...
Read now
Unlock full access