Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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

大佬总结

以上是大佬教程为你收集整理的Stream的Ruby实现全部内容,希望文章能够帮你解决Stream的Ruby实现所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: