The following does not work yet, but shows you Roughly what Base.Cartesian will get you
using MacroTools
using Base.Cartesian: @nexprs, @nloops
# @generated function many(n::NTuple{N,T}) where {N,T} # Put in a generated function like this to have it compile automatically
ex1 = @macroexpand @nextract 5 f input
ex2 = @macroexpand @nloops 5 i n->1:r_n d -> fval_d = f_d(a_{d+1}, a_d) begin
println(i_1,i_2,i_3)
end
prettify(Expr(:block, ex1,ex2)) |> display
quote
f_1 = input[1]
f_2 = input[2]
f_3 = input[3]
f_4 = input[4]
f_5 = input[5]
for i_5 = 1:r_5
fval_5 = f_5(a_6, a_5)
for i_4 = 1:r_4
fval_4 = f_4(a_5, a_4)
for i_3 = 1:r_3
fval_3 = f_3(a_4, a_3)
for i_2 = 1:r_2
fval_2 = f_2(a_3, a_2)
for i_1 = 1:r_1
fval_1 = f_1(a_2, a_1)
println(i_1, i_2, i_3)
nothing
end
nothing
end
nothing
end
nothing
end
nothing
end
end