Skip to content

Commit

Permalink
Merge pull request #958 from Mathics3/improve_switch_doc
Browse files Browse the repository at this point in the history
Improve switch doc
  • Loading branch information
rocky authored Jan 9, 2024
2 parents 4589e04 + 50ae06f commit 8be69c0
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions mathics/builtin/procedural.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,15 @@ class Switch(Builtin):
>> Switch[2, 1]
: Switch called with 2 arguments. Switch must be called with an odd number of arguments.
= Switch[2, 1]
Notice that 'Switch' evaluates each pattern before it against \
$expr$, stopping after the first match:
>> a:=(Print["a->p"];p); b:=(Print["b->q"];q);
>> Switch[p,a,1,b,2]
| a->p
= 1
>> a=.; b=.;
"""

summary_text = "switch based on a value, with patterns allowed"
Expand All @@ -486,6 +495,9 @@ def eval(self, expr, rules, evaluation):
evaluation.message("Switch", "argct", "Switch", len(rules) + 1)
return
for pattern, value in zip(rules[::2], rules[1::2]):
# The match is done against the result of the evaluation
# of `pattern`. HoldRest allows to evaluate the patterns
# just until a match is found.
if match(expr, pattern.evaluate(evaluation), evaluation):
return value.evaluate(evaluation)
# return unevaluated Switch when no pattern matches
Expand Down

0 comments on commit 8be69c0

Please sign in to comment.