Created
January 10, 2014 08:35
-
-
Save lorenzo/8348639 to your computer and use it in GitHub Desktop.
A non-recursive and stack overflow safe fibonacci function in PHP
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
| <?php | |
| function fib() { | |
| list($x, $y) = [0, 1]; | |
| while(1) { | |
| yield $y; | |
| list($x, $y) = [$y, $x + $y]; | |
| } | |
| } | |
| $generator = fib(); | |
| $i = 0; | |
| while($i++ < 10) { | |
| echo $generator->send(null) . "\n"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment