-
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
Adding array split(size:) method #199
Conversation
Codecov Report
@@ Coverage Diff @@
## master #199 +/- ##
==========================================
+ Coverage 92.84% 92.89% +0.04%
==========================================
Files 85 85
Lines 4837 4871 +34
==========================================
+ Hits 4491 4525 +34
Misses 346 346
Continue to review full report at Codecov.
|
/// | ||
/// - Parameters: | ||
/// - size: The size of the slices to be returned. | ||
public func split(size: Int) -> [ArraySlice<Element>]? { |
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.
I'm a little concerned about the memory implications of returning an ArraySlice here. I think returning an Array would be a safer API for the user. Is there a reason you chose to return an ArraySlice
?
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.
@LucianoPAlmeida
I'm also thinking this method may be better named as group(by size: Int)
. I vaguely remember discussing this when adding the groupByKey
extension recently.
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.
@SD10 Hey man :)
About the memory implications, I think it's the kind of extension that the people who use knows the concerns about ArraySlice. But i don't know, may it's something to consider.
I just return the ArraySlice because I wanted to follow the same, pattern of the native mehtods that do similar stuff like split(whereSeparator: (Element) throws -> Bool)-> [ArraySlice<Element>]
and by the way the name split is also to follow the pattern of this methods.
I thought about slices(ofSize:)
or split(ofSize:)
, let me know what you think?
public func split(size: Int) -> [ArraySlice<Element>]? { | ||
//Inspired by: https://lodash.com/docs/4.17.4#chunk | ||
guard size > 0, !isEmpty else { return nil } | ||
var value : Int = 0 |
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.
Type inference 🤔
func testSplitSize() { | ||
|
||
// A slice with value zero | ||
var array : [String] = [ "james", "irving", "jordan", "jonshon", "iverson", "shaq"] |
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.
Type inference 😅
CHANGELOG.md
Outdated
@@ -34,7 +34,7 @@ N/A | |||
- New **Array** extensions | |||
- added `groupByKey` to group the elements of the array by key in a dictionary. [#181](https://github.com/SwifterSwift/SwifterSwift/pull/181) by [@LucianoPAlmeida](https://github.com/LucianoPAlmeida) | |||
- added `forEach(slice:body:)` to iterate by specified slice size and call a closure. [#194](https://github.com/SwifterSwift/SwifterSwift/pull/194/files) by [@LucianoPAlmeida](https://github.com/LucianoPAlmeida) | |||
|
|||
- added `split(size:)` to split in an array of slices of a size. [#199](https://github.com/SwifterSwift/SwifterSwift/pull/199) by [@LucianoPAlmeida](https://github.com/LucianoPAlmeida) |
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.
Thank you 👍
Checklist