Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Find first index of occurrence of a collection in a collection.
/// Usage: myString.firstIndex(of: otherString)
extension BidirectionalCollection where Element: Equatable {
func firstIndex(of other: Self) -> Index? {
let start = other.first.flatMap(self.firstIndex(of:)),
self[start...].count >= other.count,
case let end = self.index(start, offsetBy: other.count),
zip(self[start ..< end], other).allSatisfy(==)
return nil
return start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.