Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extension CollectionType {
- func startsWith<S : SequenceType where S.Generator.Element == Generator.Element>(other: S, @noescape isEquivalent: (Generator.Element, Generator.Element) throws -> Bool) rethrows -> Index? {
- var index = self.startIndex
- // If `self` starts with `other` this will have to iterate over the whole sequence
- for element in other {
- // `self` has to be at least as long as `other`, bail out if not
- if index == self.endIndex {
- return nil
- }
- // Bail out if different
- if try isEquivalent(self[index], element) == false {
- return nil
- }
- // The elements match, continue with the next pair
- index = index.successor()
- }
- return index
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement