返回顶部

收藏

Stream的Ruby实现

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

    def empty?
        @head == nil
    end

    def head
        @head
    end

    def tail
        @tailFunc.call
    end

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

    def add(s2)
        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?
        return Stream.new(block.call(head, s2.head), lambda { tail.zip(s2.tail, &block) })
    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 Stream.new(head, lambda { tail.filter(&block) }) if block.call(head)
        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
        return 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}
        return s.head
    end

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

    def reduce(init, &block)
        return init if empty?
        return tail.reduce(block.call(init, head), &block);
    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
        return Stream.new(1, lambda{ Stream.make_ones.add(Stream.make_natural_nums) })
    end

    def Stream.make_fib_nums_drop1
        return Stream.new(1, lambda {Stream.make_fib_nums.add(Stream.make_fib_nums_drop1) })
    end

    def Stream.make_fib_nums
        return Stream.new(0, lambda { Stream.make_fib_nums_drop1 })
    end

    def Stream.make_factor
        return Stream.new(1, lambda {  Stream.make_natural_nums.drop(1).times(Stream.make_factor) })
    end
end

Stream.make_factor.take(10).print

标签:ruby

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 博主 发表 2017-12-17 12:01:00 ruby - 进行数字加减时,不要换行
  2. 博主 发表 2017-12-14 00:00:00 Ruby 2.4.3 Released
  3. 博主 发表 2017-12-14 00:00:00 Ruby 2.5.0-rc1 Released
  4. 博主 发表 2017-12-14 16:00:00 Ruby 2.2.9 Released
  5. 博主 发表 2017-12-14 16:00:00 Ruby 2.3.6 Released
  6. Wan Liuyang (t@sfdye 发表 2017-10-08 16:00:00 Creating your personal static blog with jekyll
  7. memleaks (IcebergWTF 发表 2017-12-12 05:55:41 memleaks pushed to trunk in memleaks/ruby
  8. 博主 发表 2017-12-11 14:24:00 ruby - 根据年-月来获取每个月份第一天和最后一天
  9. 博主 发表 2017-11-16 16:00:00 Ruby Web 服务器的并发模型与性能
  10. xinlu 发表 2017-11-17 02:31:32 rails数据库配置获取
  11. 博主 发表 2017-07-21 02:08:00 ruby - windows下的ruby on rails 安装
  12. 博主 发表 2017-11-22 09:56:00 ruby - 单点服务器, ruby cas server

发表评论