Instantly share code, notes, and snippets.

# HeGanjie/stream-in-ruby.md

Created February 14, 2014 12:11
Show Gist options
• Save HeGanjie/9000001 to your computer and use it in GitHub Desktop.

Stream是一种能容纳无穷对象的容器，我在研究了Js的实现后，尝试用Ruby实现一下。另外加上阶乘数列和 费波纳茨数列的实现。
Stream.js的介绍： http://www.aqee.net/stream-javascript-lib/
Stream的详情请参考： http://www.aqee.net/docs/stream/

```class Stream
def initialize(head = nil, tail = nil)
@tailFunc = tail
end

def empty?
end

end

def tail
@tailFunc.call
end

def length
count = 0
walk { count += 1}
return count
end

return zip(s2) {|x, y| x + y}
end

def substract(s2)
return zip(s2) {|x, y| x - y}
end

def times(s2)
return zip(s2) {|x, y| x * y}
end

def zip(s2, &block)
return s2 if empty?
return self if s2.empty?
end

def take(count)
return self if empty?
return Stream.new if count == 0
return Stream.new(head, lambda { tail.take(count - 1) })
end

def map(&block)
return self if empty?
return Stream.new(block.call(head), lambda { tail.map(&block) })
end

def scale(factor)
return map { |x| x * factor}
end

def filter(&block)
return self if empty?
return tail.filter(&block)
end

def drop(count)
return self if empty?
s = self
count.times {s = s.tail}
return s
end

def member?(item)
s = self
s = s.tail until s.empty? || s.head == item
end

def force
s = self
s = s.tail until (s.empty?)
end

def walk(&block)
map { |s| block.call(s); s }.force
end

def print
walk { |s| puts s}
end

def item(index)
s = self
index.times { s = s.tail}
end

def sum
return reduce(0) {|sum, new| sum + new}
end

def reduce(init, &block)
return init if empty?
end

def Stream.range(low = 1, high = nil)
return Stream.make(low) if low == high
return Stream.new(low, lambda { Stream.range(low + 1, high) })
end

def Stream.make(*args)
return Stream.new if args.empty?
return Stream.new(args.shift, lambda { Stream.make(*args) })
end

def Stream.make_ones
return Stream.new(1, lambda { Stream.make_ones })
end

def Stream.make_natural_nums
end

def Stream.make_fib_nums_drop1