This is an example I'm reading that picks out the Primes, but I don't quite see how the _? works. I haven't seen it before and can't find it in Help.
Cases[Range[500], _?PrimeQ]
This is an example I'm reading that picks out the Primes, but I don't quite see how the _? works. I haven't seen it before and can't find it in Help.
Cases[Range[500], _?PrimeQ]
This is called conditional pattern-matching.
From Professor Richard J. Gaylord's Wolfram Language Fundamentals Part One at time 57:04
See also using-a-patterntest-versus-a-condition-for-pattern-matching
Here is the main page for documentation on conditional pattern matching PatternTest
To check/test the pattern before using it somewhere, use MatchQ
_? is actually combination of two functions Blank and PatternTest. By searching the single ? in document, you'll find PatternTest. When you see unknown shorthand in Mathematica, a easy way to find their full form is to use //Hold//FullForm e.g. Cases[Range[500], _?PrimeQ] // Hold // FullForm.
– xzczd
Dec 04 '22 at 07:46
?in this context isPatternTest, and_is an ordinaryBlank[]._?fis a pattern that matches anyxsuch thatf[x]isTrue. (Note that you don't have to use_—you can use any pattern! E.g.x_Integer?PrimeQmatches anyIntegerthat is prime and names the patternx.) – thorimur Nov 12 '22 at 23:29