-
-
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
Replace Iterator#slice
and Iterator#each_slice
with Iterator#in_slices_of
#15251
Comments
module Enumerable(T)
# Returns an `Array` with chunks in the given size, eventually filled up with given value or nil.
def in_groups_of(size : Int, filled_up_with : U = nil) forall U
end
# Returns an Array with chunks in the given size. Last chunk can be smaller depending on the number of remaining items.
def in_slices_of(size : Int) : Array(Array(T))
end
end
|
I fully disagree, since it reads infinitely worse.
|
|
Changing this is a breaking change, which shouldn't just be done in a 1.x release. |
Ooo right, good point! |
Yeah thinking about it more this cannot be implemented before 2.0. It would still be nice to make note of it or add the 2.0 tag or something, because the way it currently works is inconsistent and should eventually be changed. |
I think it's unlikely that
Then the API can be fixed in 2.0 without any issues. |
I have a few issues with that, mainly that the Also, I am relatively sure there are other classes that implement a specific version of an inherited method, so I wouldn't say that |
I think Looking at other languages, the in_x_of scheme seems unconventional:
It does implement it wrongly, because it breaks the API expectations given by the docs. Just because other classes may also break API expectations doesn't mean it's correct. |
Well based on that line of thought, |
I believe this issue is essentially an instance of the more general problem described in #12803 |
So you do agree that it's not an incorrect API implementation? |
I'm actually not sure about
|
I'm not sure how that would be used in this case. Returning an array would make the following impossible: snorlax = [1, 2, 3, 4]
iterator = snorlax.each.in_groups_of(3)
iterator.next # [1, 2, 3]
iterator.next # [4, nil, nil] One would need to resort to calling |
Well of course regardless of which variant we use, every single one has the downside of not having the other possible variants available in the same way. I'm merely saying that the fact that the original collection type is an iterator should not necessarily imply all derived collection types must be iterators as well. Expecting other types can be quite reasonable. I don't mean that |
Oh yeah, totally. I wasn't arguing against that at all. My main point is that currently how But even in a case where |
Enumerable
has 2 similar methods that return anArray(Array)
of the given size, namely:For
Iterator
, these methods are not consistent.The actual method(s) that correspond to what
#in_slices_of
should be areI propose deprecating both
Iterator#slice
andIterator#each_slice
in favour ofIterator#in_slices_of
.I'd gladly implement this (as my first actual PR to Crystal 🥳) if you agree.
The text was updated successfully, but these errors were encountered: