2525 MessageException ,
2626 NegativeIntegerException ,
2727 CountableInteger ,
28+
2829)
2930from mathics .core .expression import (
3031 Expression ,
5051from mathics .core .expression import min_prec , machine_precision
5152from mathics .core .expression import structure
5253from mathics .core .evaluation import BreakInterrupt , ContinueInterrupt , ReturnInterrupt
53- from mathics .core .rules import Pattern
54+ from mathics .core .rules import Pattern , Rule
5455from mathics .core .convert import from_sympy
5556from mathics .builtin .numbers .algebra import cancel
5657from mathics .algorithm .introselect import introselect
@@ -1970,17 +1971,29 @@ def apply_pattern(self, items, sel, pattern, evaluation):
19701971class Cases (Builtin ):
19711972 """
19721973 <dl>
1973- <dt>'Cases[$list$, $pattern$]'
1974- <dd>returns the elements of $list$ that match $pattern$.
1975- <dt>'Cases[$list$, $pattern$, $ls$]'
1976- <dd>returns the elements matching at levelspec $ls$.
1974+ <dt>'Cases[$list$, $pattern$]'
1975+ <dd>returns the elements of $list$ that match $pattern$.
1976+
1977+ <dt>'Cases[$list$, $pattern$, $ls$]'
1978+ <dd>returns the elements matching at levelspec $ls$.
1979+
1980+ <dt>'Cases[$list$, $pattern$, Head->$bool$]'
1981+ <dd>Match including the head of the expression in the search.
19771982 </dl>
19781983
19791984 >> Cases[{a, 1, 2.5, "string"}, _Integer|_Real]
19801985 = {1, 2.5}
19811986 >> Cases[_Complex][{1, 2I, 3, 4-I, 5}]
19821987 = {2 I, 4 - I}
19831988
1989+ Find symbols among the elements of an expression:
1990+ >> Cases[{b, 6, \[Pi]}, _Symbol]
1991+ = {b, Pi}
1992+
1993+ Also include the head of the expression in the previous search:
1994+ >> Cases[{b, 6, \[Pi]}, _Symbol, Heads -> True]
1995+ = {List, b, Pi}
1996+
19841997 #> Cases[1, 2]
19851998 = {}
19861999
@@ -2017,17 +2030,24 @@ def apply(self, items, pattern, ls, evaluation, options):
20172030 if items .is_atom ():
20182031 return Expression (SymbolList )
20192032
2033+ from mathics .builtin .patterns import Matcher
2034+ if ls .has_form ("Rule" , 2 ):
2035+ if ls .leaves [0 ].get_name () == "System`Heads" :
2036+ heads = ls .leaves [1 ].is_true ()
2037+ ls = Expression ("List" , 1 )
2038+ else :
2039+ return evaluation .message ("Position" , "level" , ls )
2040+ else :
2041+ heads = self .get_option (options , "Heads" , evaluation ).is_true ()
2042+
20202043 try :
20212044 start , stop = python_levelspec (ls )
20222045 except InvalidLevelspecError :
20232046 return evaluation .message ("Position" , "level" , ls )
20242047
20252048 results = []
20262049
2027- from mathics .builtin .patterns import Matcher
2028-
20292050 if pattern .has_form ("Rule" , 2 ) or pattern .has_form ("RuleDelayed" , 2 ):
2030- from mathics .core .rules import Rule
20312051
20322052 match = Matcher (pattern .leaves [0 ]).match
20332053 rule = Rule (pattern .leaves [0 ], pattern .leaves [1 ])
@@ -2047,7 +2067,6 @@ def callback(level):
20472067 results .append (level )
20482068 return level
20492069
2050- heads = self .get_option (options , "Heads" , evaluation ).is_true ()
20512070 walk_levels (items , start , stop , heads = heads , callback = callback )
20522071
20532072 return Expression (SymbolList , * results )
0 commit comments