Created
November 26, 2014 14:17
-
-
Save isaiah/ee455331ff33de4c93d6 to your computer and use it in GitHub Desktop.
Lazy evaluated fibonacci sequence
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
class Fib | |
def self.fib | |
a, b = [0, 1] | |
s = 1..Float::INFINITY | |
Enumerator::Lazy.new(s.lazy) do |yielder, val| | |
a, b = b, a + b | |
yielder << a | |
end | |
end | |
end | |
puts Fib.fib.take(10).force | |
# => | |
1 | |
1 | |
2 | |
3 | |
5 | |
8 | |
13 | |
21 | |
# => | |
34 | |
55 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment