1
$\begingroup$

Here's a good reason why I think it should be,

Map[ If[AtomQ@#, #->"is an atom", #->"not an atom"]&
      , {"hello",Range@3, # &}
      , {-1}
   ]

During evaluation of In[9]:= Function::slot: Slot[(If[AtomQ[#1],#1->is an atom,#1-> not an atom]&)[1]] (in Slot[(If[AtomQ[#1],#1->is an atom,#1-> not an atom]&)[1]]&) should contain a non-negative integer or string.

Out[9]= {"hello" -> "is an atom", 
         {1 -> "is an atom", 2 -> "is an atom", 3 -> "is an atom"}, 
        Slot[(If[AtomQ[#1], #1 -> "is an atom", #1 -> " not an atom"] &)[1]] &}

Given that Slot[] is not atomic, to map a function to all atoms in an expression that contains pure functions ends up by messing up their definitions.

Edit 1 - Another Example: I have this list

l1 = {1, Range@3, Point@{2,2}}

and I want to divide all numbers by two. I can do

Map[1/2 #&, l1, {-1}]

{1/2, {1/2, 1, 3/2}, Point[{1, 1}]}

But now if instead of a Point I have a function that creates a Point

l2 = {1, Range@3, Point@{#1,#2}&}

I expect a similar result, where the argument of Point are divided by 2 i.e. Point@{1/2 #1, 1/2 #2}&. But given that Slot[n] is not an atom, that Map divides the index n inside Slot[n] by two, which does not make sense at all

Map[1/2 #&, l2, {-1}]

Function::slot: #((0.5 #1 & )[1]) (in Point[{#((0.5 #1 & )[1]), #((0.5 #1 >&)[2])}] & ) should contain a non-negative integer or string

$\endgroup$
  • $\begingroup$ I am not sure I understand why this is a good reason. You can always end up with a strange result if you don't pay attention. How about using 1& instead of #&, is that result expected? $\endgroup$ – Kuba Sep 16 '19 at 9:11
  • $\begingroup$ @Kuba As I said, the fact that Slot is not an atom messes up pure functions, any pure function, so 1& instead of #1& does not solve the general problem. See my edit for further examples. $\endgroup$ – Fortsaint Sep 16 '19 at 9:37
  • $\begingroup$ My point about 1& was that it results in possibly an unexpected result from your code, it was not meant to solve anything. I don't have an opinion here, I just said that your example didn't convince me. Btw, take a look at this 198378 thread. Among other things it discusses pros and cons of using atomic expressions. $\endgroup$ – Kuba Sep 16 '19 at 9:47
  • $\begingroup$ @Kuba Now I see what you meant. Considering my edit, where I Map the division by two to all atoms in a list conaining 1&, the latter becomes ((1*#1)/2 & )[1] & which is not clear what it means, but at least it does not give error insofar it does not mess with Slot argument (the index of Slot) $\endgroup$ – Fortsaint Sep 16 '19 at 10:13
  • $\begingroup$ 1& exhibits issue when you map inside expressions which have Hold* attributes. And ...& (Function) is HoldAll. A quick example is #^2& /@ Hold[1,2,3]. So not quite related to the original Slot problem but also showing that Map at {-1} result does not need to be 'neat'. $\endgroup$ – Kuba Sep 16 '19 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.