Skip to content

Instantly share code, notes, and snippets.

@remcopeereboom
Last active August 29, 2015 14:15
Show Gist options
  • Save remcopeereboom/1829f3d6be4a61efc570 to your computer and use it in GitHub Desktop.
Save remcopeereboom/1829f3d6be4a61efc570 to your computer and use it in GitHub Desktop.
A naive ruby port of my lisp code for implementing streams.
class EvenStream
attr_reader :value
def initialize(n = 0)
@value = n
@next = lambda { EvenStream.new(n + 2) }
end
def next
@next.call
end
end
class FibonacciStream
attr_reader :value
def initialize(previous = 1, value = 1)
@previous = previous
@value = value
@next = lambda { FibonacciStream.new(value, previous + value) }
end
def next
@next.call
end
end
e = FibonacciStream.new
10.times do
p e.value
e = e.next
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment