Created
September 14, 2017 13:25
-
-
Save ssanderson/84ddcc57def34e9f2684b8dd9df122be to your computer and use it in GitHub Desktop.
"Functional" Switchcase Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from codetransformer.transformers.switchcase import switch | |
def foo(x): | |
print("Start: x={}".format(x)) | |
with switch(x) as case: | |
@case(1) | |
def _1(): | |
# Read and write x. | |
x = 'x was ' + str(x) | |
@case(2) | |
def _2(): | |
# Introduce a new "local". | |
y = 'introduced y' | |
@case(3) | |
def _3(): | |
# See the mutations from cases 1 and 2. | |
z = {'x': x, 'y': y} | |
break_ # Break out of the case construct. | |
@case(4) | |
def _4(): | |
assert False, "Shouldn't be hit." | |
# See the mutations introduced in the cases. | |
print("End: x={!r}, y={!r}, z={!r}".format(x, y, z)) | |
foo(1) | |
# Prints: | |
# -- Start: x=1 | |
# -- End: x='x was 1', y='introduced y', z={'y': 'introduced y', 'x': 'x was 1'} | |
foo(2) | |
# Prints: | |
# -- Start: x=2 | |
# -- End: x=2, y='introduced y', z={'y': 'introduced y', 'x': 2} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment