Skip to content
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

Array form of #each_slice #12844

Closed
HertzDevil opened this issue Dec 15, 2022 · 0 comments · Fixed by #13108
Closed

Array form of #each_slice #12844

HertzDevil opened this issue Dec 15, 2022 · 0 comments · Fixed by #13108
Labels
good first issue This is an issue suited for newcomers to become aquianted with working on the codebase. kind:feature topic:stdlib:collection

Comments

@HertzDevil
Copy link
Contributor

#in_groups_of has an Array form, a block form, and an Iterator form:

[1, 2, 3].in_groups_of(2)                           # => [[1, 2], [3, nil]]
[1, 2, 3].in_groups_of(2) { |e| p e if nil.in?(e) } # => [3, nil]
[1, 2, 3].each.in_groups_of(2).next                 # => [1, 2]

#each_slice only has the last two:

[1, 2, 3].each_slice(2) { |e| p e if e.size == 1 } # => [3]
[1, 2, 3].each.slice(2).next                       # => [1, 2]
[1, 2, 3].each_slice(2).next                       # => [1, 2] # `Iterable` alias

There should be a method to fulfil that missing form, say:

module Enumerable(T)
  def in_slices_of(count : Int) : Array(Array(T))
  end
end

[1, 2, 3].in_slices_of(2) # => [[1, 2], [3]]
@HertzDevil HertzDevil changed the title Array form of Enumerable#each_slice Array form of #each_slice Dec 15, 2022
@HertzDevil HertzDevil added the good first issue This is an issue suited for newcomers to become aquianted with working on the codebase. label Feb 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue This is an issue suited for newcomers to become aquianted with working on the codebase. kind:feature topic:stdlib:collection
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant