Skip to content

Instantly share code, notes, and snippets.

@guzhenhuaGitHub
Created September 21, 2018 03:47
Show Gist options
  • Save guzhenhuaGitHub/ac6c8e033c17c4863b3fde0918c54e41 to your computer and use it in GitHub Desktop.
Save guzhenhuaGitHub/ac6c8e033c17c4863b3fde0918c54e41 to your computer and use it in GitHub Desktop.
helper like ruby's Enumerable#each_cons
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