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 <= p partition(p, a[2:end], (a, l...), r) else partition(p, a[2:end], l, (a, r...)) end end function select(k, a) println(k, a) if length(a) == 1 a else p = a 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))