5 ms·
I actually found an immense use for Walrus - used it to hack Python into doing pattern matching that is way more readable than without it: https://github.com/e
by kolinko 7y ago
I actually found an immense use for Walrus - used it to hack Python into doing pattern matching that is way more readable than without it:
https://github.com/eveem-org/panoramix https://github.com/eveem-org/panoramix
(source code for Eveem.org, which is arguably the best decompiler for Ethereum smart contracts out there. you can see a lot of pattern matching in pano/simplify.py , and I found no way to do it without extending the language/walrus while maintaining the readability)
- gizmo385 7y agoAfter a quick read through, I personally find this syntax much harder to read using operator overloads than it would have been using a series of comparison functions. Tilde is a very obtuse operator and I don't know that this really buys you anything. Feels like a case of preferring cleverness over readability/usability/maintainability.
- mark-r 7y agoExactly the road C++ tries to go down. How's that for a ringing endorsement?
- wolco 7y agoC++ is one of the most successful languages.
- nine_k 7y agoDespite its flaws, not due to them. Same can be said about e.g. Cobol.
- eru 7y agoDespite the flaws, and because of being in the right place at the right time. Cobol also was a good idea back in the day. And some of the ideas in Cobol we know recognize as bad _had_ to be tried to gain that recognition.
- otabdeveloper4 7y agoFalse. There have been dozens of working C++ competitors over the years, each one technically as good as C++. They all failed because their authors are good at writing languages, but very bad at knowing how languages are used in the real world. The C++ committee really knows what it's doing, you're not going to do anything useful in this space without actually understanding the problem domain. TL;DR - yes, C++ is complex, but that is because programming is complex. Your attitude of "programming is hard, let's go (language) shopping" will not displace C++.
- nine_k 7y agoC++ made a number of... unfortunate decisions early in its development, which resulted in a much worse language than it could have been. (Hindsight is 20/20, of course.) I totally agree that C++ of 21st century is way better than C++ from 1998, and is becoming better. The problem is that maintaining backwards compatibility requires to keep a number of footguns in place, and some of them are cornerstones of the language :( I have high hopes for Rust becoming the more sane competitor in the space traditionally occupied by C++. Its authors seem to understand the problem domain from a very practical standpoint, and are proficient in C++, to begin with. They also strive to apply reasonable design principles, and some actual math hopefully will keep the core of the language from logical pitfalls. Another example of hitting a sweet spot is Go. I dislike many Go's language decisions. (Some are definitely great, though, e.g. the whole OOP approach lifted from Oberon.) But its authors definitely understand both the problem domain, and the target audience. This is what I mean by talking about success despite the flaws. C++ has both flaws and merits, and merits outweighed the flaws. But the flaws are still painful.
- raobit 7y agoRust:- Am i a joke to you?
- mark-r 7y agoHow much of that success can be attributed to the features that were added in the last 10 years?
- cortesoft 7y agoPretty good?
- MichaelMoser123 7y agoPython is adding features, maybe they want to be as expressive as perl, when they grow up; however that comes with the price of decreased readability.
- kolinko 7y agoI actually had a series of comparison functions initially, but those weren't that readable. To give you an example, let's look at this statement: if exp ~ ('mask_shl', int:size, int:offset, -offset, :e): ... It is the same as writing: if type(exp) == tuple and len(exp) == 5 and exp[0]=='mask_shl' and type(exp[1])==int aand type(exp[2])==int and exp[3] == -exp[1]: size, offset, e = exp[1], exp[2], exp[4] ... If you can figure out syntax that makes this sort of matches more readable, I'll gladly use it :)
- kolinko 7y agoFixing formatting for above: if exp ~ ('mask_shl', int:size, int:offset, -offset, :e): ... vs if type(exp) == tuple and len(exp) == 5 and exp[0]=='mask_shl' and type(exp[1])==int aand type(exp[2])==int and exp[3] == -exp[1]: size, offset, e = exp[1], exp[2], exp[4] ...
- closed 7y agoThis is super interesting! I've never seen codecs used to introduce language features like this... Do you have any good references for getting started with using codecs? Also, I wonder if there's an easy way to implement this in a jupyter notebook, without fiddling with the kernel... (issues of whether it's a good idea aside, definitely seems useful to know about.. )
- kolinko 7y agoThanks! The idea for using codecs is not mine - found it on StackOverflow somewhere. Googling for "how to add custom language statements python" should give you some results, this included :) Also, in my case I had to use Codecs because a new operator causes AST-parser to not go through. But if you have some kind of syntax that will be compatible with Python AST, then you can implement it even easier. You can google Python AST to get a lot of useful information :)
- onlyrealcuzzo 7y agoJust a suggestion... I went to your repo, and it wasn't clear to me what Panoramix actually is or does. So I checked out Eveem.org, and even on the about page, it wasn't clear to me what Eveem was. But with that context, coming back to your GitHub page, I was able to get the gist of it. It might be helpful to have a section above the installation section that gives a little context / tells about the project. And in your example section -- again, I literally don't know anything about decompilation so maybe I'm not your target demographic... but if I understand the gist of it, kitties is an example function or binary. It might be better to use something more concrete (and provide the context).
- kolinko 7y agoThank for the feedback! My first open-source project. I will improve this :)
- ohazi 7y agoYou should link to a snippet that actually uses := rather than to the top level project.
- kolinko 7y agoAh yes, sorry: https://github.com/eveem-org/panoramix/blob/bc55be84a9a6abb17e2de5052097611c595643ee/pano/simplify.py#L183 https://github.com/eveem-org/panoramix/blob/bc55be84a9a6abb1... It uses "~" custom operator, but implementing it would not be possible without ":=" operator (see /tilde directory).
- deleted 7y ago[deleted]
- Tommah 7y agoThere is a similar trick that you can use without the walrus operator. I wrote the code many years ago and I am reproducing this from memory, so the code below may be a little off. Define this class: class Any: def __eq__(self, other): self.value = other return True Then you can use an instance of it as the hole in a pattern. My use case was performing peephole optimizations in a simple 6502 assembler. For example, if the LDA operation (load value from memory location into accumulator) occurs twice in a row, the first load is redundant. So I did this test: x, y = Any(), Any() if L[i:i+2] == [('LDA', x), ('LDA', y)]: L[i:i+2] = [('LDA', y.value)] (Note that the last line changes the length of the list, so it may be slow on large lists.) I was going to write a blog post about this back then, but I never did. If anyone is interested, I can write it now.
- eru 7y agoBut that breaks normal equality tests for your type?
- heavenlyblue 7y agoWouldn’t parent’s code simply copy into Any every time it’s compared in a list? It is some spooky action at a distance.
- eru 7y agoProbably. I found it a bit confusing in any case.
- deleted 7y ago[deleted]
- m463 7y agoI thought regex matching was one of the strongest arguments for it.