Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
each_cons from Ruby in Swift as a function on Sequence
extension Array {
func eachConsecutive(_ n: Int) -> Array<ArraySlice<Iterator.Element>> {
return (0..<(count-n+1)).map({ i in
return self[i..<i+n]
extension Sequence where SubSequence: Sequence, SubSequence.Iterator.Element == Iterator.Element {
func eachPair() -> AnySequence<(Iterator.Element, Iterator.Element)> {
return AnySequence(zip(self, self.dropFirst()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment