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.



195
196
197
198
# File 'lib/stream.rb', line 195

def initialize(seq)
  @seq = seq
  set_to_begin
end

Instance Attribute Details

#posObject (readonly)

Returns the value of attribute pos.



192
193
194
# File 'lib/stream.rb', line 192

def pos
  @pos
end

Instance Method Details

#at_beginning?Boolean

Returns:

  • (Boolean)


204
205
206
# File 'lib/stream.rb', line 204

def at_beginning?
  @pos < 0
end

#at_end?Boolean

Returns:

  • (Boolean)


200
201
202
# File 'lib/stream.rb', line 200

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

#basic_backwardObject



223
224
225
226
# File 'lib/stream.rb', line 223

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

#basic_forwardObject



218
219
220
221
# File 'lib/stream.rb', line 218

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

#set_to_beginObject

positioning



210
211
212
# File 'lib/stream.rb', line 210

def set_to_begin
  @pos = -1
end

#set_to_endObject



214
215
216
# File 'lib/stream.rb', line 214

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