-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add guard clause to Deque#rotate! #5399
Conversation
|
<= 1 is probably better. |
If you were checking for being empty, I think |
Rotating an empty list or a list of one does not change the list, so we return early in those cases. This also fixes a DivisionByZeroError when @SiZe == 0.
9985d87
to
fb8caae
Compare
Thanks for the feedback everyone! I went with the |
Welcome and congratulations for your first PR @willcosgrove 🎉 |
Rotating an empty list or a list of one does not change the list, so we return early in those cases. This also fixes a DivisionByZeroError when @SiZe == 0.
This is my first time contributing to Crystal, so please let me know if there's something I should change. I went back and forth on writing the guard clause as
@size == 0
orempty?
.shift
andpop
used@size == 0
.halfs
usedempty?
.On one hand, the error this is fixing is due to
@size
being zero. But on the other hand, it makes the most intuitive sense to me that we're short circuiting the rotation when the Deque is empty, because an empty Deque (or Array or whatever) rotated is still an empty Deque.Come to think of it, we can actually return early if
@size <= 1
because a Deque of size 1 rotated any amount is still the same value.Would you like me to change this guard clause to
@size <= 1
, leave it as is, or change it to@size == 0
?Fixes #5393