Skip to content

Instantly share code, notes, and snippets.

@daehn
Last active August 25, 2015 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daehn/610357e581f6fbf4512d to your computer and use it in GitHub Desktop.
Save daehn/610357e581f6fbf4512d to your computer and use it in GitHub Desktop.
Swift Generator that produces Fibonacci numbers
import UIKit
struct FibonacciGenerator: GeneratorType, SequenceType {
var (f1, f2) = (1, 1)
mutating func next() -> Int? {
let result = f1
(f1, f2) = (f2, f1 + f2)
return result
}
func generate() -> FibonacciGenerator {
return FibonacciGenerator()
}
}
var generator = FibonacciGenerator().generate()
for _ in 1...20 {
generator.next()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment