-
-
Save lilyball/7c4b1fe013223a068fe3 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 { | |
var elements : S.GeneratorType | |
mutating func next() -> Int? { | |
while true { | |
switch elements.next() { | |
case let .Some(x) where x % 2 == 0: continue | |
case let .Some(x): return x * 5 | |
case .None: return nil | |
} | |
} | |
} | |
init(_ seq: S) { | |
self.elements = seq.generate() | |
} | |
} | |
let xs = [1, 5, 8, 3] | |
let gen = OddNumbersTimesFiveGenerator(xs) | |
let seq = SequenceOf<Int>({ gen }) | |
let seq2 = map(filter(xs, { $0 % 2 != 0 }), { $0 * 5 }) | |
println(equal(seq, seq2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment