Last active
August 25, 2015 22:19
-
-
Save daehn/610357e581f6fbf4512d to your computer and use it in GitHub Desktop.
Swift Generator that produces Fibonacci numbers
This file contains 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
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