Skip to content

Instantly share code, notes, and snippets.

@isaiah
Created November 26, 2014 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaiah/ee455331ff33de4c93d6 to your computer and use it in GitHub Desktop.
Save isaiah/ee455331ff33de4c93d6 to your computer and use it in GitHub Desktop.
Lazy evaluated fibonacci sequence
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