Class: Stream::CollectionStream

Inherits:
BasicStream show all
Defined in:
lib/stream.rb

Overview

A CollectionStream can be used as an external iterator for each interger-indexed collection. The state of the iterator is stored in instance variable @pos.

A CollectionStream for an array is created by the method Array#create_stream.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Stream

#+, #backward, #collect, #concatenate, #concatenate_collected, #create_stream, #current, #current_edge, #each, #empty?, #filtered, #first, #forward, #last, #modify, #move_backward_until, #move_forward_until, #peek, #remove_first, #remove_last, #reverse, #unwrapped

Methods included from Enumerable

#create_stream

Constructor Details

#initialize(seq) ⇒ CollectionStream

Creates a new CollectionStream for the indexable sequence seq.



146
147
148
149
# File 'lib/stream.rb', line 146

def initialize(seq)
  @seq = seq
  set_to_begin
end

Instance Attribute Details

#posObject (readonly)

Returns the value of attribute pos.



143
144
145
# File 'lib/stream.rb', line 143

def pos
  @pos
end

Instance Method Details

#at_beginning?Boolean

Returns:

  • (Boolean)


152
# File 'lib/stream.rb', line 152

def at_beginning?; @pos < 0; end

#at_end?Boolean

Returns:

  • (Boolean)


151
# File 'lib/stream.rb', line 151

def at_end?; @pos + 1 >= @seq.size; end

#basic_backwardObject



161
# File 'lib/stream.rb', line 161

def basic_backward; r = @seq[@pos]; @pos -= 1; r; end

#basic_forwardObject



160
# File 'lib/stream.rb', line 160

def basic_forward; @pos += 1; @seq[@pos]; end

#set_to_beginObject



157
# File 'lib/stream.rb', line 157

def set_to_begin; @pos = -1; end

#set_to_endObject



158
# File 'lib/stream.rb', line 158

def set_to_end; @pos = @seq.size - 1; end