3 ms·
Sorry, I should have been clearer. The macro way of doing it doesn't mess with stack traces as it expands to pretty clean scheme code, at least after the optimi
by bjoli 8y ago
Sorry, I should have been clearer. The macro way of doing it doesn't mess with stack traces as it expands to pretty clean scheme code, at least after the optimizer has had it's way.
I am sad that most languages don't have macros since it is a very powerful tool. Not only does it let you abstract the ugliness away from approaches like this python one (which is actually pretty decent), but it also let's you produce good machine code from a "bolted on" construct. The python matcher is bound to have some overhead, whereas a syntactically expanded one is not.
- marmaduke 8y agoAh I see what you mean (I wrote macros in Clojure years ago). Yep that would do it, but would require an AST-rewriting decorator in Python.
- uranusjr 8y agoThat’s an interesting thought! I guess I just found a good project to play on during the holidays. I wonder if something like this would work: with matching(expr) as m: @m(pattern) def do_for_pattern(v): ... @m(_) def catchall(v): ...
- btown 8y agoOoh that is a really nice syntax. And it would definitely work as it is essentially: m(pattern)(lambda v: ...) Design would be to consider the execution of the match and dispatch as a “cleanup” of the specification created in the block. Which makes a ton of sense in a weird way. May end up using this, or one of us should send a PR to the OP!
- b3orn 8y agoSomething like this? class matching: def __init__(self, expr): self.expr = expr self.patterns = [] self.result = None def __call__(self, pattern): def _inner(func): self.patterns.append((pattern, func)) return func return _inner def __enter__(self): return self def __exit__(self, *args): for pattern, func in self.patterns: if pattern.match(self.expr): self.result = func(self.expr)