Created
June 19, 2014 00:40
-
-
Save atg/0c3896103d29629a5e70 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
struct OddNumbersTimesFiveGenerator<S : Sequence where S.GeneratorType.Element == Int> : Generator { | |
typealias Element = Int | |
var elements : S.GeneratorType | |
mutating func next() -> Int? { | |
while (true) { | |
if let x = elements.next() { | |
if x % 2 == 0 { continue } | |
else { return x * 5 } | |
} | |
else { return nil } | |
} | |
} | |
init(_ seq: S) { | |
self.elements = seq.generate() | |
} | |
} | |
let xs = [1, 5, 8, 3] | |
let gen = OddNumbersTimesFiveGenerator(xs) | |
let seq = SequenceOf<Int>({ gen }) | |
for x in seq { | |
println(String(x)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment