Skip to content

Instantly share code, notes, and snippets.

@austinzheng
Created June 9, 2016 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save austinzheng/dbee353163eb22920d7a567c2366058f to your computer and use it in GitHub Desktop.
Save austinzheng/dbee353163eb22920d7a567c2366058f to your computer and use it in GitHub Desktop.
extension Sequence {
typealias E = Iterator.Element
typealias I = Iterator
public func prefix(while predicate: (E) -> Bool) -> UnfoldSequence<E, I> {
return sequence(state: makeIterator(), next: { (s: inout I) -> E? in
guard let next = s.next() else {
return nil
}
return predicate(next) ? next : nil
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment