(In case this is version specific I am using Mathematica 10.1 under Windows.)
While responding to Partition array without unpacking I noticed something about MapAt
that I don't believe I have before:
pQ = Developer`PackedArrayQ;
x = Range @ Range @ 3
pQ[x]
Map[pQ, x]
MapAt[pQ, x, 2]
{{1}, {1, 2}, {1, 2, 3}} False {True, True, True} {{1}, False, {1, 2, 3}}
Note the last output line: the second element is False
rather than True
. The element {1, 2}
has apparently been unpacked by MapAt
before the function that is mapped ever sees it. Also notably the other elements are not unpacked:
pQ /@ MapAt[pQ, x, 2] (* {True, False, True} *)
It is understandable that it would unpack a list which it was going to operate within , e.g. MapAt[f, x, {2, 1}]
but not a list passed verbatim to the operating function. I cannot think of a good reason for MapAt
to unpack in this case, and without a counterargument I would consider it a bug.
Is this a bug or oversight, or is there a reason for this behavior that I am failing to consider?
MapAt
takes complex position specification, evenSpan
is allowed. It would take relatively complex logic to figure out what to unpack and what to leave packed, so they didn't implement that. At least this is why I didn't write about MapAt when I complained about Partition and Append to support. It seems there's still plenty of opportunity for optimization. $\endgroup$MapAt
started by unpacking everything would have figured it was a design decision along those lines, but since it manages to keep elements one and three in this example packed it is obviously possible to unpack only some elements and not others. I do not see the need/reason for unpacking the deepest level. I don't see howSpan
factors into this. If we useMapAt[pQ, x, 2 ;; 3]
(which also unpacks) it is apparent that neither two nor three need to be unpacked. (continued) $\endgroup$