2
$\begingroup$

That's the input I've put

dagny[x_] := x^2;
  Map[dagny, {4, 9, {8, {5}}, {2, 3}}, {2}]

Output:

{4, 9, {64, {25}}, {4, 9}}

But this not I want. I wanna {4, 9, {64, {5}}, {4, 9}}

How to Map that at a specific Level?

$\endgroup$
2
$\begingroup$
ClearAll[dagny]
dagny[x : Except[_List]] := x^2;
dagny[x_] := x

Map[dagny, {4, 9, {8, {5}}, {2, 3}}, {2}]

{4, 9, {64, {5}}, {4, 9}}

$\endgroup$
0
$\begingroup$

Do you mean this?

MapAt[dagny, {4, 9, {8, {5}}, {2, 3}}, {{3, 1}, {4}}]
$\endgroup$
5
  • $\begingroup$ Nope. Map at a specific level. You're trying to do with the position in a list. What I want here is a a level stuff. $\endgroup$ – kile Jan 29 '20 at 6:16
  • 1
    $\begingroup$ @kile, then you need to modify the function you're mapping: Map[If[! ListQ[#], #^2, #] &, {4, 9, {8, {5}}, {2, 3}}, {2}] $\endgroup$ – J. M.'s ennui Jan 29 '20 at 6:19
  • 1
    $\begingroup$ @J.M. For more generality, I recommend AtomQ. $\endgroup$ – bcegkmqs23 Jan 29 '20 at 6:28
  • $\begingroup$ @bcegkmqs23 how to achieve that with AtomQ? can u write it in the answer? $\endgroup$ – kile Jan 29 '20 at 6:34
  • 1
    $\begingroup$ @kile, Compare and contrast: Map[If[AtomQ[#], #^2, #] &, {4, 9, {8, {5}}, {2, 3}}, {2}]. Alternatively, you could modify the definition of your function to be mapped: dagny[x_?AtomQ] := x^2; dagny[x_] := x. $\endgroup$ – J. M.'s ennui Jan 29 '20 at 6:41

Your Answer

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

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