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};
  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;

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}];

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.

  • 1
    $\begingroup$ Given list = {a, b, c}, what outcome would you expect? $\endgroup$ Commented Dec 11, 2018 at 13:32
  • $\begingroup$ {{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. $\endgroup$
    – NeverMind
    Commented Dec 11, 2018 at 13:37
  • $\begingroup$ The question is more conceptual based, it is not a question of how to get the same answer in a different way. $\endgroup$
    – NeverMind
    Commented Dec 11, 2018 at 13:39
  • $\begingroup$ Then why is {list} not acceptable? $\endgroup$ Commented Dec 11, 2018 at 13:40
  • 5
    $\begingroup$ 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 ? $\endgroup$
    – Kuba
    Commented Dec 11, 2018 at 13:45

1 Answer 1


Maybe use Unevaluated or pass a function:

Map[Table[#, {i, Length[list]}] &, {Unevaluated[list[[i]]]}]
Map[Table[#[i], {i, Length[list]}] &, {list[[#]] &}]
  • $\begingroup$ Yes I think that comes close to what I need. Thank you! $\endgroup$
    – NeverMind
    Commented Dec 11, 2018 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.