I want to define a symbol and use it within a function. For example, with IDnumbers defined as a list of numbers:
ParallelMap[{#1, Name[#1], Age[#1]} &, IDnumbers]
With userlist={#1, Name[#1], Age[#1]} becomes:
ParallelMap[userlist &, IDnumbers]
It works just fine with the list itself in the code, but not with the symbol. The same thing happens with a list of strings vs. a symbol assigned to a list of strings. Why is this?
Since
f[#]&is shorthand forFunction[f[#]]you should always complete your anonymous function with a trailing&to get a working function.In your example:
More thorough explanation:
By just using something like
f[#]you get (inFullForm[])whereas this gets transformed to a Function by the trailing
&operator:If you do this in two steps,
&doesn’t evaluate the intermediate variableexpr:You can force the evaluation of
exprbefore it gets wrapped in theFunction[]by usingEvaluate[]:Another way is to supply the
Function[]wrapper yourself:Personally, i would consider this bad coding style. Just get used to always finishing an anonymous function with a trailing
&like you would supply a closing paranthesis)to a corresponding opening one(.Edit
Ok, in your case of a dynamically generated anonymous function i can see why you couldn’t supply the
&directly. Just wrap the expression with theSlot[]s in aFunction[]instead.