# How do I map a function onto the result of an expression that already has a pure function as an argument

I admit that I have only a basic understanding of the way Mathematica handles pure functions - enough to be dangerous, unfortunately.

I have some data that is in a list of lists of lists. I would like to apply a function over a subset of each of those (top level) lists, and I'm wondering if there is a way to do it with mapping rather than using Table. In the following example I have three lists of ordered pairs. I'd like to quickly select a subset of each of those three top level lists by testing a property of the deepest level elements. Doing it for one of the three is easy:

testList = Table[Table[{i, RandomReal[]}, {i, 100}], {j, 1, 3}];
subset1 = Select[testList[], PrimeQ[#[]]&];


What I'm wondering is whether or not there is some way to do something like this:

subsetList = Select[<argument>, PrimeQ[#[]]&] & /@ testList


where "argument" is some sort of indicator that it should be mapped "outside" to the testList level. I'm sure there's some obvious use of Apply or some such that can do this, I just can't quite figure it out!

• I don't know if those are good enough to be duplicates: 16947, 35322.
– Kuba
Apr 9, 2015 at 5:59
• Why not just Select[#, PrimeQ[#[]]&] & /@ testList?
– kglr
Apr 9, 2015 at 13:03
• This works, but I certainly did not think it would until you suggested it. I guess it's because each "&" denotes a separate rule? If you can help me understand why this works and doesn't confuse the "#"s I'd appreciate it. I'm accepting a slightly different answer since it seems more general. Apr 9, 2015 at 19:25

Use the pure function in the form of Function[x, ...] or Function[{x, y}, ...] for more arguments. For example,

Function[arg, Select[arg, PrimeQ[#[]] &]] /@ testList

• This not only works, but is also easily readable and parse-able by my tiny coding brain. Thanks! Apr 9, 2015 at 19:29

Here is another way using Cases

 Cases[#, {x_, y___} /; PrimeQ[x] :> {x, y}] & /@ testList


Versification with Map method given by Taiki

testList = Table[Table[{i, RandomReal[]}, {i, 10}], {j, 1, 3}];
Function[arg, Select[arg, PrimeQ[#[]] &]] /@ testList
Cases[#, {x_, y___} /; PrimeQ[x] :> {x, y}] & /@ testList Just to illustrate (using testList definition given) :

c1 = Cases[#, {_?PrimeQ, _}, Infinity] & /@ testList;
c2 = Select[#, PrimeQ[First@#] &] & /@ testList;
c3 = Pick[#, Map[Function[x, PrimeQ[First@x]], #]] & /@ testList;


and for the ridiculous:

c4 = Flatten[Last[Reap[Sow[{##}, #1], _?PrimeQ, #2[] &]]] /. {} ->
Sequence[] & @@@ # & /@ testList;


Note:

And[c1 == c2, c2 == c3, c3 == c4]


yields True