In Files

Included Modules

Stream

Module Stream defines an interface for an external Iterator which can move forward and backwards. See README for more information.

The functionality is similar to Smalltalk's ReadStream.

Public Instance Methods

+(otherStream) click to toggle source

Create a Stream::ConcatenatedStream by concatenatating the receiver and otherStream

(%w(a b c).create_stream + [4,5].create_stream).to_a ==> ["a", "b", "c", 4, 5]
# File lib/stream.rb, line 537
def + (otherStream)
      [self, otherStream].create_stream.concatenate
end
at_beginning?() click to toggle source

Returns false if the next backward will return an element.

# File lib/stream.rb, line 20
def at_beginning?; raise NotImplementedError; end
at_end?() click to toggle source

Returns false if the next forward will return an element.

# File lib/stream.rb, line 17
def at_end?; raise NotImplementedError; end
backward() click to toggle source

Move backward one position. Returns the source of current_edge. Raises Stream::EndOfStreamException if at_beginning? is true.

# File lib/stream.rb, line 31
def backward
  raise EndOfStreamException if at_beginning?
  basic_backward
end
collect(&mapping) click to toggle source

Create a Stream::MappedStream wrapper on self. Instead of returning the stream element on each move, the value of calling mapping is returned instead. See Stream::MappedStream for examples.

# File lib/stream.rb, line 520
def collect (&mapping); MappedStream.new(self, &mapping); end
concatenate() click to toggle source

Create a Stream::ConcatenatedStream on self, which must be a stream of streams.

# File lib/stream.rb, line 523
def concatenate; ConcatenatedStream.new self; end
concatenate_collected(&mapping) click to toggle source

Create a Stream::ConcatenatedStream, concatenated from streams build with the block for each element of self:

s = [1, 2, 3].create_stream.concatenate_collected { |i|
  [i,-i].create_stream
}.
s.to_a ==> [1, -1, 2, -2, 3, -3]
# File lib/stream.rb, line 532
def concatenate_collected (&mapping); self.collect(&mapping).concatenate; end
create_stream() click to toggle source

create_stream is used for each Enumerable to create a stream for it. A Stream as an Enumerable returns itself.

# File lib/stream.rb, line 116
def create_stream; self end
current() click to toggle source

Returns the element returned by the last call of forward. If at_beginning? is true self is returned.

# File lib/stream.rb, line 84
def current; at_beginning? ? self : basic_current; end
current_edge() click to toggle source

Returns the array [current,peek].

# File lib/stream.rb, line 91
def current_edge; [current,peek]; end
each() click to toggle source

Implements the standard iterator used by module Enumerable, by calling set_to_begin and basic_forward until at_end? is true.

# File lib/stream.rb, line 107
def each
  set_to_begin
  until at_end?
    yield basic_forward
  end
end
empty?() click to toggle source

Returns true if the stream is empty which is equivalent to at_end? and at_beginning? both being true.

# File lib/stream.rb, line 103
def empty?; at_end? and at_beginning?; end
filtered(&block) click to toggle source

Return a Stream::FilteredStream which iterates over all my elements satisfying the condition specified by the block.

# File lib/stream.rb, line 512
def filtered (&block); FilteredStream.new(self,&block); end
first() click to toggle source

Returns the first element of the stream. This is accomplished by calling set_to_begin and forward, which means a state change.

# File lib/stream.rb, line 95
def first; set_to_begin; forward; end
forward() click to toggle source

Move forward one position. Returns the target of current_edge. Raises Stream::EndOfStreamException if at_end? is true.

# File lib/stream.rb, line 24
def forward
  raise EndOfStreamException if at_end?
  basic_forward
end
last() click to toggle source

Returns the last element of the stream. This is accomplished by calling set_to_begin and backward, which means a state change.

# File lib/stream.rb, line 99
def last; set_to_end; backward; end
modify(&block) click to toggle source

Create a Stream::ImplicitStream which wraps the receiver stream by modifying one or more basic methods of the receiver. As an example the method remove_first uses modify to create an ImplicitStream which filters the first element away.

# File lib/stream.rb, line 544
def modify (&block); ImplicitStream.new(self, &block); end
move_backward_until() click to toggle source

Move backward until the boolean block is not false and returns the element found. Returns nil if no object matches.

# File lib/stream.rb, line 74
def move_backward_until
      until at_beginning?
        element = basic_backward
        return element if yield(element)
      end
      nil
end
move_forward_until() click to toggle source

Move forward until the boolean block is not false and returns the element found. Returns nil if no object matches.

This is similar to detect, but starts the search from the current position. detect, which is inherited from Enumerable uses each, which implicitly calls set_to_begin.

# File lib/stream.rb, line 64
def move_forward_until
      until at_end?
        element = basic_forward
        return element if yield(element)
      end
      nil
end
peek() click to toggle source

Returns the element returned by the last call of backward. If at_end? is true self is returned.

# File lib/stream.rb, line 88
def peek; at_end? ? self : basic_peek; end
remove_first() click to toggle source

Returns a Stream::ImplicitStream wrapping a Stream::FilteredStream, which eliminates the first element of the receiver.

(1..3).create_stream.remove_first.to_a ==> [2,3]
# File lib/stream.rb, line 550
def remove_first
      i = 0
      filter = self.filtered { | element | i += 1; i > 1 }
      filter.modify { |s|
        s.set_to_begin_proc = proc {filter.set_to_begin; i = 0}
      }
end
remove_last() click to toggle source

Returns a Stream which eliminates the first element of the receiver.

(1..3).create_stream.remove_last.to_a ==> [1,2]

Take a look at the source. The implementation is inefficient but elegant.

# File lib/stream.rb, line 563
def remove_last
      self.reverse.remove_first.reverse      # I like this one
end
reverse() click to toggle source

Create a Stream::ReversedStream wrapper on self.

# File lib/stream.rb, line 515
def reverse; ReversedStream.new self; end
set_to_begin() click to toggle source

Position the stream before its first element, i.e. the next forward will return the first element.

# File lib/stream.rb, line 38
def set_to_begin
  until at_beginning?; basic_backward; end
end
set_to_end() click to toggle source

Position the stream behind its last element, i.e. the next backward will return the last element.

# File lib/stream.rb, line 44
def set_to_end
  until at_end?; basic_forward; end
end
unwrapped() click to toggle source

A Stream::WrappedStream should return the wrapped stream unwrapped. If the stream is not a wrapper around another stream it simply returns itself.

# File lib/stream.rb, line 120
def unwrapped; self; end

Protected Instance Methods

basic_backward() click to toggle source
# File lib/stream.rb, line 51
def basic_backward; raise NotImplementedError; end
basic_current() click to toggle source
# File lib/stream.rb, line 53
def basic_current; backward; forward; end
basic_forward() click to toggle source
# File lib/stream.rb, line 50
def basic_forward; raise NotImplementedError; end
basic_peek() click to toggle source
# File lib/stream.rb, line 54
def basic_peek; forward; backward; end

[Validate]

Generated with the Darkfish Rdoc Generator 2.