Skip to content

Instantly share code, notes, and snippets.

Created January 16, 2016 21:25
Show Gist options
  • Save anonymous/3ebf1fee6bfbe28db196 to your computer and use it in GitHub Desktop.
Save anonymous/3ebf1fee6bfbe28db196 to your computer and use it in GitHub Desktop.
extension CollectionType where Generator.Element : Equatable {
func objectNear(object: Self.Generator.Element, indexModifier: Index.Distance) -> Self.Generator.Element? {
if let index = self.indexOf(object) {
let limit = indexModifier>0 ? self.indices.endIndex : self.indices.startIndex
let newIndex = index.advancedBy(indexModifier, limit: limit)
if self.indices.contains(newIndex) {
return self[newIndex]
}
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment