first steps to metaprogramming

The first thing you should ask yourself when writing metaprogramming is “why do I need metaprogramming for this”. In this example, the function:

function wp_loop(x, n::Int)
    v  = 1.0
    for i in 1:n 
        v *= (x-i)
    end
    return v
end

or perhaps

function wp_prod(x, n::Int)
    prod(i -> x-i, 1:n)
end

evaluates the polynomial without metaprogramming. If you really need to specialize on n you can do

function wp_val(x, ::Val{n}) where {n}
    v  = 1.0
    for i in 1:n 
        v *= (x-i)
    end
    return v
end

and get the specialized:

julia> @code_llvm wp_vaö(4.0, Val(3))
  %1 = fadd double %0, -1.000000e+00
  %2 = fadd double %0, -2.000000e+00
  %3 = fmul double %1, %2
  %4 = fadd double %0, -3.000000e+00
  %5 = fmul double %3, %4
  ret double %5
}

If this is just a learning exercise and you want to create a function based on an expression, you could do:

julia> @eval function fp_4(x)
           $(wp(4))
       end
fp_4 (generic function with 1 method)

julia> fp_4(7)
360
5 Likes