Class: Stream::FilteredStream
- Inherits:
-
WrappedStream
- Object
- BasicStream
- WrappedStream
- Stream::FilteredStream
- Defined in:
- lib/stream.rb
Overview
A FilteredStream selects all elements which satisfy a given booelan block of another stream being wrapped.
A FilteredStream is created by the method #filtered:
(1..6).create_stream.filtered { |x| x % 2 == 0 }.to_a ==> [2, 4, 6]
Instance Attribute Summary
Attributes inherited from WrappedStream
Instance Method Summary collapse
- #at_beginning? ⇒ Boolean
-
#at_end? ⇒ Boolean
at_end? has to look ahead if there is an element satisfing the filter.
- #basic_backward ⇒ Object
- #basic_forward ⇒ Object
-
#initialize(otherStream, &filter) ⇒ FilteredStream
constructor
Create a new FilteredStream wrapping otherStream and selecting all its elements which satisfy the condition defined by the block_filter_.
-
#pos ⇒ Object
Returns the current position of the stream.
- #set_to_begin ⇒ Object
- #set_to_end ⇒ Object
Methods inherited from WrappedStream
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
Constructor Details
#initialize(otherStream, &filter) ⇒ FilteredStream
Create a new FilteredStream wrapping otherStream and selecting all its elements which satisfy the condition defined by the block_filter_.
246 247 248 249 250 251 |
# File 'lib/stream.rb', line 246 def initialize(otherStream, &filter) super otherStream @filter = filter @positionHolder = IntervalStream.new set_to_begin end |
Instance Method Details
#at_beginning? ⇒ Boolean
253 |
# File 'lib/stream.rb', line 253 def at_beginning?; @positionHolder.at_beginning?; end |
#at_end? ⇒ Boolean
at_end? has to look ahead if there is an element satisfing the filter
256 257 258 259 260 261 262 263 264 265 |
# File 'lib/stream.rb', line 256 def at_end? @positionHolder.at_end? and begin if @peek.nil? @peek = wrapped_stream.move_forward_until( &@filter ) or return true @positionHolder.increment_stop end false end end |
#basic_backward ⇒ Object
280 281 282 283 284 285 |
# File 'lib/stream.rb', line 280 def basic_backward wrapped_stream.backward unless @peek.nil? @peek = nil @positionHolder.backward wrapped_stream.move_backward_until(&@filter) or self end |
#basic_forward ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/stream.rb', line 267 def basic_forward result = if @peek.nil? wrapped_stream.move_forward_until(&@filter) else # Do not move!! @peek end @peek = nil @positionHolder.forward result end |
#pos ⇒ Object
Returns the current position of the stream.
299 |
# File 'lib/stream.rb', line 299 def pos; @positionHolder.pos; end |
#set_to_begin ⇒ Object
292 293 294 295 296 |
# File 'lib/stream.rb', line 292 def set_to_begin super @peek = nil @positionHolder.set_to_begin end |
#set_to_end ⇒ Object
287 288 289 290 |
# File 'lib/stream.rb', line 287 def set_to_end # Not super which is a WrappedStream, but same behavior as in Stream until at_end?; basic_forward; end end |