Skip to content

Instantly share code, notes, and snippets.

@jtbandes
Last active August 29, 2015 14:03
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 jtbandes/da1039b04d3f89fe4b73 to your computer and use it in GitHub Desktop.
Save jtbandes/da1039b04d3f89fe4b73 to your computer and use it in GitHub Desktop.
func sift<T, S: Sequence where S.GeneratorType.Element == Optional<T>>(xs: S) -> GeneratorOf<T> {
var gen = xs.generate()
return GeneratorOf<T> {
var next: T??
do {
next = gen.next()
if !next { return nil }
} while !(next!)
return next!
}
}
let opts: [Int?] = [1, 3, nil, 4, 7]
[Int](sift(opts)) // [1, 3, 4, 7]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment