Skip to content

Instantly share code, notes, and snippets.

@abriening
Created May 12, 2009 14:20
Show Gist options
  • Save abriening/110511 to your computer and use it in GitHub Desktop.
Save abriening/110511 to your computer and use it in GitHub Desktop.
class Stream
include Enumerable
attr_reader :succ, :value
def initialize(value, &block)
@value, @succ = value, block
end
def next
Stream.new( succ.call( value ) , &succ )
end
def [](index)
stream = self
index.times{ stream = stream.next }
stream.value
end
def each(&block)
stack_safe = proc do | stream |
block.call stream.value
stream.next
end
stream = self
loop{ stream = stack_safe.call(stream) }
end
end
# s = Stream.new(1){ |n| n + 1 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment