Last active
August 22, 2021 09:16
-
-
Save jaekong/59d57ed787aac2ebcdea4da0911dac5a to your computer and use it in GitHub Desktop.
Info: This is now irrelevant since Swift Algorithms package now contains a chunks(ofCount:) method. A simple Swift extension to extend Collection protocol to have a split(into:) function. It is useful when you have to chunk an array or data to equal sized pieces.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Info: This is now irrelevant since Swift Algorithms package now contains a chunks(ofCount:) method. | |
extension Collection where Index == Int { | |
func split(into size: Int) -> [Self.SubSequence] { | |
return stride(from: 0, to: count, by: size).map { | |
self[$0 ..< Swift.min($0 + size, count)] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment