//there is no "yield" for a plain
//sequence: "sequence" builds a lazy
//generator out of a closure
let counter = sequence(state: 3) {
(current: inout Int) -> Int? in
defer { current += 2 }
return current <= 9 ? current : nil
}
for c in counter {
print(c)
}
//printed 3, 5, 7, 9
//AsyncStream is the closest thing
//to a real "yield"
func makeCounter(low: Int, high: Int,
step: Int) -> AsyncStream<Int> {
AsyncStream { continuation in
var current = low
while current <= high {
continuation.yield(current)
current += step
}
continuation.finish()
}
}
for await c in makeCounter(
low: 3, high: 9, step: 2) {
print(c)
}