Live data from Hacker News

Ask HN: Do you ever go back and admire a piece of code you wrote?

news.ycombinator.com

21–30 of 267 posts

Re: Ask HN: Do you ever go back and admire a piece of code you wrote?

#22
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

Re: Ask HN: Do you ever go back and admire a piece of code you wrote?

#25
post #22

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

Re: Ask HN: Do you ever go back and admire a piece of code you wrote?

#27
post #8

Yes. Early on in my career, I needed a function that produced a range of dates, given a start and end date. After it was all said and done, it boiled out like so: def daterange(start,end): while start Though simplistic and straightforward, I admire the solution. I am sure there may be “better” ways to achieve the same result, and posting this here may result in cowboys trashing it or pointing out a fallacy - oh well.…

Are you proud of using a generator to create dates as you need? Just trying to understand the logic

Re: Ask HN: Do you ever go back and admire a piece of code you wrote?

#28
I admire my old code when I go back to that code and by reading the code, see some idiosyncratic behaviour. Then a pay slightly more attention and see my own comment in the code saying that the author knows about it and that was the assumption made in the first place.

Otherwise code can always be made better.

Re: Ask HN: Do you ever go back and admire a piece of code you wrote?

#29
post #8

Yes. Early on in my career, I needed a function that produced a range of dates, given a start and end date. After it was all said and done, it boiled out like so: def daterange(start,end): while start Though simplistic and straightforward, I admire the solution. I am sure there may be “better” ways to achieve the same result, and posting this here may result in cowboys trashing it or pointing out a fallacy - oh well.…

Not trying to trash it, but doing

  dt = timedelta(days=1)
outside the loop and using it inside would make it a bit more efficient.
Post reply on HN