yattee/Extensions/Array+Next.swift

14 lines
303 B
Swift
Raw Normal View History

2021-06-28 10:43:07 +00:00
extension Array where Element: Equatable {
func next(after element: Element) -> Element? {
let idx = firstIndex(of: element)
if idx == nil {
return first
}
let next = index(after: idx!)
return self[next == endIndex ? startIndex : next]
}
}