Created
August 10, 2016 19:07
-
-
Save sanghapark/0733232b6ec369004f60c696a031b4e3 to your computer and use it in GitHub Desktop.
generate fibonacci numbers using Swift
This file contains hidden or 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
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