Rearrange graph

I have to rearrange graph described by

j = {1 -> 2, 2 -> 3, 3 -> 1};


applying some function to all vertices on the right so rearrange[j] output should give me

{1 -> f[2], 2 -> f[3], 3 -> f[1]}


I guess I can use ReplaceAll, but can't guess how.

You can use MapAt function to map function on specific part of the expression

MapAt[f, {1 -> 2, 2 -> 3, 3 -> 1}, {All, 2}]
(* ==> {1 -> f[2], 2 -> f[3], 3 -> f[1]} *)


or use replacement rule

{1 -> 2, 2 -> 3, 3 -> 1} /. Rule[a_, b_] :> Rule[a, f[b]]


Anotner way,

#1 -> f[#2]& @@@ {1 -> 2, 2 -> 3, 3 -> 1}


yields:

{1 -> f[2], 2 -> f[3], 3 -> f[1]}

• I think that brackets are unnecessary. Just #1 -> f[#2] & @@@. Oct 15 '13 at 10:15
• Yes! sorry...silly mistake...have corrected Oct 15 '13 at 11:10

Here is another way:

j={1->2,2->3,3->1};
j[[All,2]]=f/@j[[All,2]];
j


{1 -> f[2], 2 -> f[3], 3 -> f[1]}

A little bit more compact method:

MapAt[f, #, 2] & /@ {1 -> 2, 2 -> 3, 3 -> 1}

{1 -> f[2], 2 -> f[3], 3 -> f[1]}


For versions 10+:

Normal[f /@ Association[j]]


{1 -> f[2], 2 -> f[3], 3 -> f[1]}

Also

KeyValueMap[# -> f @ #2 &] @ Association[j]


{1 -> f[2], 2 -> f[3], 3 -> f[1]}

ReplacePart[j, {p_,-1}:>f[j[[p,-1]]]]


{1 -> f[2], 2 -> f[3], 3 -> f[1]}