# Use iterator outside of iteration

I want to Map something that is depending on an iterator from the outside of e.g. Table into the procedure.

What would be the correct way to do this in Mathematica without evoking error messages.

Here is a simple example of what I am trying to do:

list = {a, b, c};
Map[
Table[#, {i, Length[list]}] &,
{ list[[i]] }
]


Since my initial post was a little bit unclear to what I really intend to do, I want to provide another, hopefully clearer example. I am currently experimenting a lot with MMA's Compile function and wondered how one could define a function outside of a compiled function that will be called within the body of the compiled function and may depend on an iteration variable that is counted withing the Compile enviroment. I then stumbled over this thread here which was helpful.

A minimum working example of what I intend to do would be

v[x_] := Part[x, i];

cf = Hold@
Compile[{{n, _Integer}},
Block[{v, x}, x = Range[n]; Sum[v[x], {i, 1, n}]],
CompilationTarget :> "C", RuntimeOptions -> "Speed"] //.
DownValues@v // ReleaseHold;

Needs["CompiledFunctionTools"];
Grid[{{CompilePrint[cf]}}, Frame -> All]


where now the compiled functions compiles through without any calls to MainEvaluate.

On the other hand, as is stated in the linked thread one can typically inline pure external functions into a compiled function by using the option "InlineExternalDefinitions"->True. However, in this case this strategy doesn't seem to be successful. Something like

v = Part[#1, #2] &;

cf2 = Compile[{{n, _Integer}},
Block[{v, x}, x = Range[n]; Sum[v[x, i], {i, 1, n}]],
CompilationTarget :> "C", RuntimeOptions -> "Speed",
CompilationOptions -> {"InlineExternalDefinitions" -> True}];

Needs["CompiledFunctionTools"];
Grid[{{CompilePrint[cf2]}}, Frame -> All]


won't compile through without calls to MainEvaluate while something like

g = #^2 &;
cf3 = Compile[{{x, _Real}}, g[x], CompilationTarget -> "C",
CompilationOptions -> {"InlineExternalDefinitions" -> True}]


does compile through.

Of course, one can still wonder why I wouldn't inject the definition of v[x] directly into the compiled function. But for my purposes it is more convenient to have one compiled function and change the function v[x] outside of the compiled function where I can now use different function definitions of v[x] for the same compiled function.

• Given list = {a, b, c}, what outcome would you expect? – Αλέξανδρος Ζεγγ Dec 11 '18 at 13:32
• {{a,b,c}} in this case, but I want to avoid the error message Part::pspec: Part specification i is neither a machine-sized integer nor a list of machine-sized integers. >>. Tried to use Hold and ReleaseHold but maybe I misplaced it. The list[[i]] shouldn't be evaluated before it is stuffed into the Table. – Display Name Dec 11 '18 at 13:37
• The question is more conceptual based, it is not a question of how to get the same answer in a different way. – Display Name Dec 11 '18 at 13:39
• Then why is {list} not acceptable? – Αλέξανδρος Ζεγγ Dec 11 '18 at 13:40
• I am sorry but this does not make sense to me. What do you want to Map? Don't you need a nested Table or MapIndexed instead ? – Kuba Dec 11 '18 at 13:45

Maybe use Unevaluated or pass a function:
Map[Table[#, {i, Length[list]}] &, {Unevaluated[list[[i]]]}]