Module | Stream |
In: |
lib/stream.rb
|
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.
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]
Move backward one position. Returns the source of current_edge. Raises Stream::EndOfStreamException if at_beginning? is true.
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.
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]
create_stream is used for each Enumerable to create a stream for it. A Stream as an Enumerable returns itself.
Returns the element returned by the last call of forward. If at_beginning? is true self is returned.
Implements the standard iterator used by module Enumerable, by calling set_to_begin and basic_forward until at_end? is true.
Returns true if the stream is empty which is equivalent to at_end? and at_beginning? both being true.
Return a Stream::FilteredStream which iterates over all my elements satisfying the condition specified by the block.
Returns the first element of the stream. This is accomplished by calling set_to_begin and forward, which means a state change.
Move forward one position. Returns the target of current_edge. Raises Stream::EndOfStreamException if at_end? is true.
Returns the last element of the stream. This is accomplished by calling set_to_begin and backward, which means a state change.
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.
Move backward until the boolean block is not false and returns the element found. Returns nil if no object matches.
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.
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]
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.
Position the stream before its first element, i.e. the next forward will return the first element.
Position the stream behind its last element, i.e. the next backward will return the last element.
A Stream::WrappedStream should return the wrapped stream unwrapped. If the stream is not a wrapper around another stream it simply returns itself.