Skip to content

Instantly share code, notes, and snippets.

@hlung
Created May 3, 2020 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hlung/dfb7b9d61a216dec2fbaf5e07a4a4c06 to your computer and use it in GitHub Desktop.
Save hlung/dfb7b9d61a216dec2fbaf5e07a4a4c06 to your computer and use it in GitHub Desktop.
Iterate through all elements in pair tuples
import Foundation
public extension Array {
// Iterate through all elements in pair tuples
// e.g. [1, 2, 3, 4].allPairs = [(1, 2), (2, 3), (3, 4)]
var allPairs: [(Element, Element)] {
var array: [(Element, Element)] = []
for i in 0..<self.count - 1 {
array.append((self[i], self[i+1]))
}
return array
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment