Not entirely mine but beautiful nonetheless: @dataclass class Node: id: str children: List[Node] = field(default_factory=list) # beautiful Python: traverse a tree depth-first, pre-order (stack based) def __iter__(self): stack = [self] while stack: node = stack.pop() yield node stack = node.children + stack
You should really use deque for the stack
Ask HN: Do you ever go back and admire a piece of code you wrote?
91–100 of 267 posts
Re: Ask HN: Do you ever go back and admire a piece of code you wrote?
#92Re: Ask HN: Do you ever go back and admire a piece of code you wrote?
#93Earlier quoted context omitted.
Except for the return to happen when that's true, you'd need an await. At least in JS.
Not if it's returned by an async function. In JS, async functions automatically await promises that are returned.
The main difference would be error handling: if you have a catch block that returns a different value, returning the promise directly would actually throw in the calling function, whereas if you "return await", it's going to get caught in your catch block, resolving the promise to your own custom value.
Found a detailed explanation here: https://jakearchibald.com/2017/await-vs-return-vs-return-awa...
Re: Ask HN: Do you ever go back and admire a piece of code you wrote?
#94The only thing I have ever gone back and admired is when I wrote the following snippet of code: long time; // no see
// http://photos3.meetupstatic.com/photos/event/4/b/c/2/600_436939394.jpeg
I don't know if anyone ever noticed this comment and followed the URL, but I hope they did and were amused. (The code was eventually refactored/rewritten, so the comment no longer exists in the codebase.)Re: Ask HN: Do you ever go back and admire a piece of code you wrote?
#95we call this code review
Re: Ask HN: Do you ever go back and admire a piece of code you wrote?
#96All the time. The old joke is about us looking at code we wrote six months ago, and being horrified at what we see. And I've had more than my share of that. But what's ALSO happened, many times, is I look at old code I wrote... and I'm IMPRESSED. Just blown away by the beautiful elegance and power of the abstractions I came up with, the sheer intelligence of the approach, the insight and lucidity oozing from the code…
Me writing docstrings: God this is so much work for nothing... my code should speak for itself. Me 6 months later: Thank god I wrote docstrings, what is this garbled mess.
To each their own, but comments are generally a positive addition to code.
Re: Ask HN: Do you ever go back and admire a piece of code you wrote?
#97Re: Ask HN: Do you ever go back and admire a piece of code you wrote?
#98During my first tour with the company I built, among other things:
- (on a two-person team) an ASP.NET web app that allows payroll admins to extract year-end data from their AP in order to distribute tax forms (W-2s, 1099s, etc.)
- (different two-person team) a mail-room management back-end that allowed us to print/mail over 1 million forms/year for customers of the above
- (yet another two-person team) A corporate intranet product
Since returning to the company I've been on another small team (3 developers) building an independent payroll platform. As part of that effort, we've had to integrate with all the other things I ever built at the company. It's been extremely valuable for our team that I have intimate knowledge of basically all of our integration points across the company. Much of my original comments and code from ~2007-2015 are still in the source tree. Some of that makes me happy, and some of that makes me cringe :)