Skip to content

Instantly share code, notes, and snippets.

@sanghapark
Created August 10, 2016 19:07
Show Gist options
  • Save sanghapark/0733232b6ec369004f60c696a031b4e3 to your computer and use it in GitHub Desktop.
Save sanghapark/0733232b6ec369004f60c696a031b4e3 to your computer and use it in GitHub Desktop.
generate fibonacci numbers using Swift
protocol GeneratorType {
associatedtype Element
mutating func next() -> Element?
}
class FibsGenerator: GeneratorType {
var state = (0, 1)
func next() -> Int? {
let upcomingNumber = state.0
state = (state.1, state.0 + state.1)
return upcomingNumber
}
}
let gen = FibsGenerator()
while (true) {
print(gen.next())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment