6
$\begingroup$

I want to map the function to the value part of these rules and get the following result:

{1 -> f[1, 2], 2 -> f[2, 3], 3 -> f[3, 4], 4 -> f[4, 5], 5 -> f[5, 6],
  6 -> f[6, 7], 7 -> f[1, 8], 8 -> f[8, 9], 9 -> f[9, 10], 
 10 -> f[10, 11], 11 -> f[11, 12], 12 -> f[7, 12], 13 -> f[2, 8], 
 14 -> f[3, 9], 15 -> f[4, 10], 16 -> f[5, 11], 17 -> f[6, 12], 
 18 -> f[2, 9], 19 -> f[3, 10], 20 -> f[5, 10], 21 -> f[6, 11]}

But I can't implement the requirements by the following ways:

pole = {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {1, 8}, {8, 
        9}, {9, 10}, {10, 11}, {11, 12}, {7, 12}, {2, 8}, {3, 9}, {4, 
        10}, {5, 11}, {6, 12}, {2, 9}, {3, 10}, {5, 10}, {6, 11}};
Map[f, Thread[Range[Length[pole]] -> pole], {2}]

What can I do to meet this requirement?

$\endgroup$
10
$\begingroup$
MapIndexed[#2[[1]] -> f @@ # &, pole]
{1 -> f[1, 2], 2 -> f[2, 3], 3 -> f[3, 4], 4 -> f[4, 5], 5 -> f[5, 6],
   6 -> f[6, 7], 7 -> f[1, 8], 8 -> f[8, 9], 9 -> f[9, 10], 
   10 -> f[10, 11], 11 -> f[11, 12], 12 -> f[7, 12], 13 -> f[2, 8], 
   14 -> f[3, 9], 15 -> f[4, 10], 16 -> f[5, 11], 17 -> f[6, 12], 
   18 -> f[2, 9], 19 -> f[3, 10], 20 -> f[5, 10], 21 -> f[6, 11]}

Also

MapIndexed[#2[[1]] -> # &, f @@@ pole]

Thread[Range[Length[pole]] -> f @@@ pole]

Normal @ f @@@ AssociationThread[Range[Length[pole]], pole] 

rules = Thread[Range[Length[pole]] -> pole];
Normal @ f @@@ Association @ rules

MapAt[Apply[f], rules, {All, 2}]
| improve this answer | |
$\endgroup$

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.