# Mapping a function with levelspec vs. Mapping over a flattened list

I have borrowed the following cyclic permutations list making function from here.

CyclicPermutations[list_] := Map[RotateRight[list, #] &, Range[Length[list]] - 1]


Using this function I make a list of cyclic permutations of the digits of some prime numbers as follows:

Table[CyclicPermutations[IntegerDigits[Prime[n]]], {n, 100, 101}]

{{{5, 4, 1}, {1, 5, 4}, {4, 1, 5}}, {{5, 4, 7}, {7, 5, 4}, {4, 7, 5}}}


Reconstructing the list of numbers using Map[f,list,levelspec] and FromDigits gives:

Map[FromDigits,Table[CyclicPermutations[IntegerDigits[Prime[n]]], {n, 100, 101}], 1]

{{514, 451, 145}, {574, 457, 745}}


The integers have been permuted again in the reconstruction. However, the following code does not permute them again at the cost of losing needed structure:

FromDigits/@Flatten[Table[CyclicPermutations[IntegerDigits[Prime[n]]], {n, 100, 101}], 1]

{541, 154, 415, 547, 754, 475}


I could partition this list properly but it would be very nice to keep the original nested structure.

edit:

I changed my function slightly to:

CyclicPermutations[list_]:=Table[RotateRight[list, n],{n, 1, Length[list]}]


An implementation of Project Euler # 35

count = 0;
Table[If[AllTrue[FromDigits/@CyclicPermutations[IntegerDigits[Prime[n]]],PrimeQ],count+=1],{n, 1, 78498}];
count

55


You can use Map directly in your table. In your second line use:

Table[FromDigits /@ CyclicPermutations[IntegerDigits[Prime[n]]], {n, 100, 101}]


{{541, 154, 415}, {547, 754, 475}}

The issue with your Map[FromDigits, ...] solution is that you call FromDigits for a list of lists, instead of simple list of digits. FromDigits seems to have some undocumented threading syntax:

FromDigits[{{1, 2, 3}, {1, 2, 3}, {1, 2, 3}, {1, 2, 3}}]


{1111, 2222, 3333}

You can Map FromDigits[Transpose @ #] & at level 1:

Map[FromDigits[Transpose@#] &,
Table[CyclicPermutations[IntegerDigits[Prime[n]]], {n, 100, 101}], 1]


{{541, 154, 415}, {547, 754, 475}}

Alternatively, you can Map FromDigits at level {2}

Map[FromDigits, Table[CyclicPermutations[IntegerDigits[Prime[n]]], {n, 100,
101}], {2}]


{{541, 154, 415}, {547, 754, 475}}

You can also use:

Map[FromDigits, Transpose /@
Table[CyclicPermutations[IntegerDigits[Prime[n]]], {n, 100, 101}], 1]


{{541, 154, 415}, {547, 754, 475}}

FromDigits[Transpose[#]] & /@
Table[CyclicPermutations[IntegerDigits[Prime[n]]], {n, 100, 101}]


{{541, 154, 415}, {547, 754, 475}}

You might be looking for MapAt:

MapAt[FromDigits, Table[CyclicPermutations[IntegerDigits[Prime[n]]], {n, 100, 101}], {All, All}]


{{541, 154, 415}, {547, 754, 475}}

{All, All} here means that FromDigits is applied to all elements in the second dimension. {All, 1} would apply it to the first element of all elements in in the second dimension. {1, All} would apply it to all of the elements of the first dimension.