Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have to rearrange graph described by

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

appling 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.

share|improve this question
1  
Related, more general: mathematica.stackexchange.com/questions/5740/… –  Pinguin Dirk Oct 15 '13 at 10:25
add comment

4 Answers

up vote 8 down vote accepted

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]]
share|improve this answer
add comment

Anotner way,

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

yields:

{1 -> f[2], 2 -> f[3], 3 -> f[1]}
share|improve this answer
1  
I think that brackets are unnecessary. Just #1 -> f[#2] & @@@. –  ybeltukov Oct 15 '13 at 10:15
    
Yes! sorry...silly mistake...have corrected –  ubpdqn Oct 15 '13 at 11:10
add comment

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]}

share|improve this answer
add comment

A little bit more compact method:

MapAt[f, #, 2] & /@ {1 -> 2, 2 -> 3, 3 -> 1}
{1 -> f[2], 2 -> f[3], 3 -> f[1]}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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