I am trying to understand the limits of what the compiler can do when functions are defined with a lot of compile-time information, like tuples of fixed length, or also static arrays.
In this example, we have a selection algorithm that allows us to compute the median from an unsorted input. When the input is small, say 5 or 7 values, it would be great if inlining might output code where we only have a lot of comparisons until we reach the answer in the end, with no intermediate function calls.
This is not what happens, though. Can I modify this program somehow to obtain this resul? Or maybe this is a case I should use a generated function?
partition(p, aa) = partition(p, aa, (), ())
function partition(p, a, l, r)
if length(a) == 0
(l, r)
elseif a[1] <= p
partition(p, a[2:end], (a[1], l...), r)
else
partition(p, a[2:end], l, (a[1], r...))
end
end
function select(k, a)
println(k, a)
if length(a) == 1
a[1]
else
p = a[1]
l, r = partition(p, a)
println(l, r)
if length(l) > k
select(k, l)
elseif length(l) < k
select(k - length(l), r)
else # length(l)==k
p
end
end
end
@code_native select(2, (7,8,4))