Last active
August 29, 2015 14:15
-
-
Save remcopeereboom/1829f3d6be4a61efc570 to your computer and use it in GitHub Desktop.
A naive ruby port of my lisp code for implementing streams.
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
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