Created
September 21, 2018 03:47
-
-
Save guzhenhuaGitHub/ac6c8e033c17c4863b3fde0918c54e41 to your computer and use it in GitHub Desktop.
helper like ruby's Enumerable#each_cons
This file contains hidden or 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
extension Collection { | |
func eachConsecutive(_ size: Int) -> [[Element]] { | |
let droppedIndices = indices.dropFirst(size - 1) | |
return zip(indices, droppedIndices) | |
.map { | |
return Array(self[$0...$1]) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment