Subset iterator
Maybe you could do something like:
X = 3; Y = 5; Table[l[i] = Indexed[g, i], {i, X}]; Table[{l[1], l[3]}, {g, Subsets[Range[Y],{X}]}] {{1, 3}, {1, 4}, {1, 5}, {1, 4}, {1, 5}, {1, 5}, {2, 4}, {2, 5}, {2, 5}, {3, 5}}
You could create a function to do this:
SetAttributes[iteratedTable,HoldAll] iteratedTable[e_, g_Symbol, iterations_, max_] := Block[{g = Indexed[\[FormalL], #]&}, Table[e, {\[FormalL], Subsets[Range@max, {iterations}]}] ] Simple example:
iteratedTable[h[l[1], l[3], l[2]], l, 3, 5] {h[1, 3, 2], h[1, 4, 2], h[1, 5, 2], h[1, 4, 3], h[1, 5, 3], h[1, 5, 4], h[2, 4, 3], h[2, 5, 3], h[2, 5, 4], h[3, 5, 4]}
Your examples:
progress = 0; iteratedTable[progress++, l, 2, 40]; progress 780
and:
progress = 0; iteratedTable[progress++, l, 3, 40]; progress 9880
Nested iterators
An alternate approach that is similar to yours is:
SetAttributes[iteratedTable, HoldAll] iteratedTable[e_, g_, iter_, max_] := ReleaseHold @ Hold[Table][ Hold[e], Sequence @@ Table[{g[i], If[i==1, 1, g[i-1]+1], max}, {i, iter}] ] Avoiding indexed variables
If you must have symbols instead of indexed variables, I think you should give the function the list of symbols. So:
SetAttributes[iteratedTable,HoldAll] iteratedTable[e_, g:{__Symbol}, max_] := Block[g, g = Thread[Indexed[\[FormalL], Range@Length@g]]; Table[e, {\[FormalL], Subsets[Range@max, {Length@g}]}] ] Examples:
iteratedTable[h[a,c], {a,b,c}, 5] {h[1, 3], h[1, 4], h[1, 5], h[1, 4], h[1, 5], h[1, 5], h[2, 4], h[2, 5], h[2, 5], h[3, 5]}
progress=0; iteratedTable[progress++, {l1, l2, l3}, 40]; progress 9880