Last active
August 29, 2015 14:03
-
-
Save jtbandes/da1039b04d3f89fe4b73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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