class Stream

Attributes

head[R]

Public Class Methods

continually() { || ... } click to toggle source
# File lib/ruby-stream.rb, line 2
def self.continually(&block)
  Stream.new(yield) do
    Stream.continually(&block)
  end
end
new(head, &block) click to toggle source
# File lib/ruby-stream.rb, line 10
def initialize(head, &block)
  @head = head
  @tail_block = block
end

Public Instance Methods

[](n) click to toggle source
# File lib/ruby-stream.rb, line 28
def [](n)
  if n == 0
    self.head
  elsif n < 0
    nil
  else
    last_stream = self
    n.times {
      return nil if last_stream.kind_of?(EmptyStream)
      last_stream = last_stream.tail
    }

    last_stream.head
  end
end
drop(n) click to toggle source
# File lib/ruby-stream.rb, line 81
def drop(n)
  if n <= 0
    Stream.new(head) do
      tail
    end
  else
    tail.drop(n - 1)
  end
end
each(&block) click to toggle source
# File lib/ruby-stream.rb, line 50
def each(&block)
  last_stream = self

  until last_stream.kind_of?(EmptyStream)
    block.call(last_stream.head)
    last_stream = last_stream.tail
  end

  nil
end
last() click to toggle source
# File lib/ruby-stream.rb, line 19
def last
  result = nil
  self.each do |ele|
    result = ele
  end

  result
end
length() click to toggle source
# File lib/ruby-stream.rb, line 44
def length
  counter = 0
  self.each { |ele| counter += 1 }
  counter
end
map() { |head) do map(&block)| ... } click to toggle source
# File lib/ruby-stream.rb, line 91
def map(&block)
  Stream.new(yield head) do
    tail.map(&block)
  end
tail() click to toggle source
# File lib/ruby-stream.rb, line 15
def tail
  @tail_block.call
end
take(n) click to toggle source
# File lib/ruby-stream.rb, line 61
def take(n)
  if n <= 0
    EmptyStream.new
  else
    Stream.new(head) do
      tail.take(n - 1)
    end
  end
end
take_while(&block) click to toggle source
# File lib/ruby-stream.rb, line 71
def take_while(&block)
  if block.call(head)
    Stream.new(head) do
      tail.take_while(&block)
    end
  else
    EmptyStream.new
  end
end