Created
February 11, 2021 23:51
-
-
Save sug0/c69ec7ef37c300f725573e4141a5c486 to your computer and use it in GitHub Desktop.
PEP pattern matching is useless
This file contains hidden or 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
| def match(value, clauses): | |
| for m in clauses: | |
| try: | |
| exec(f'{m} = {value}') | |
| return clauses[m](locals()) | |
| except (TypeError, ValueError): | |
| continue | |
| raise ValueError('non exhaustive patterns') | |
| def main(): | |
| value = match((5, 1, 3), { | |
| '(x, y)': lambda env: env['x'], | |
| '(x, y, z)': lambda env: env['z'], | |
| }) | |
| print(value) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment