Skip to content

Instantly share code, notes, and snippets.

@lilyball
Forked from atg/gist:0c3896103d29629a5e70
Last active August 29, 2015 14:02
Show Gist options
  • Save lilyball/7c4b1fe013223a068fe3 to your computer and use it in GitHub Desktop.
Save lilyball/7c4b1fe013223a068fe3 to your computer and use it in GitHub Desktop.
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