New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Explain how Length.run_step works #1608
Conversation
6eba5e3
to
6f37801
Compare
I definitely like making this clearer! I've got two small comments:
|
The idea is that we are repeatedly trying to delete But before doing so, we first “skip over” all of the rightmost items that we have previously failed to delete during this pass, delete items from the end of whatever remains, and then append all of the “skipped” items after the deleted items before considering the new candidate list. Reusing your example, we would eventually skip over the stubborn Eventually we find that 0 is also stubborn, so our skipped region contains 0 and 2. That’s now the entire sequence, so the pass ends. |
(I’m not 100% on the name, because it doesn’t quite capture that we tried to delete each stubborn element before giving up and deciding to skip it for the remainder of the pass.) |
Another way of looking at it is that in the original code, In the renamed code, These two lengths sum to the total length of the current sequence. When we take the slice (Ideally that latter slice would be |
) | ||
) | ||
j += 1 | ||
# Skip over the element we couldn't delete, and continue. | ||
# (If we deleted everything, this will put j > len(self.current), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops, this reference to j
no longer makes sense now that I've renamed the variables.
This comment has been minimized.
This comment has been minimized.
6f37801
to
acad9ca
Compare
I tightened up some of the text, and moved the diagram into a “footnote” at the bottom of the file, so there's less of a wall-of-text effect when reading through the implementation from top to bottom. I also changed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fantastic explanation - LGTM.
This PR (hopefully) makes it easier to understand how
Length.run_step
works.First it replaces
j
andi + 1
with more meaningful variable names, based on my interpretation of what's happening.Then it adds an overall description of the shrinking process, step-by-step comments, and a diagram to put each variable and slice into context.
This is a stand-alone change that doesn't fix or change any behaviour, so there's no particular hurry to get it merged.
I recommend looking the variable-renaming commit on its own, because the combined diff makes it a bit harder to see that the new version is equivalent.