Can logical operators be used with Cases?
Generally I would use Condition
:
Cases[Range[-75, 100], x_ /; Positive[x] && PrimeQ[x]]
For the specific case of And
you can string PatternTest
if you control grouping:
Cases[Range[-75, 100], (_?Positive)?PrimeQ]
{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}
Cases[Range[-75, 100], _?(Positive[#] && PrimeQ[#] &)]
(* or *)
Cases[Range[-75, 100], _?(Apply[And, Composition[Positive[#], PrimeQ[#]]] &)]
(* which is the same as the one below*)
Cases[Range[-75, 100], _?(Apply[And, Positive[#1]@*PrimeQ[#1]] &)]
(* or even more compactly *)
Cases[Range[-75, 100], _?(And @@ Positive[#1]@*PrimeQ[#1] &)]
a different way with Cases
:
(Cases[#, _?Positive] ⋂ Cases[#, _?PrimeQ]) &@Range[-75, 100]
You can use operator forms with Select
:
Select[
Range[-75,100],
Through @* And[Positive, PrimeQ]
]
{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}