yattee/Extensions/Array+Next.swift

18 lines
367 B
Swift
Raw Normal View History

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