Class: Stream::MappedStream

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

Overview

The analog to Enumerable#collect for a stream is a MappedStream wrapping another stream. A MappedStream is created by the method #collect, thus modifying the behavior mixed in by Enumerable:

(1..5).create_stream.collect {|x| x**2}.type ==> Stream::MappedStream
(1..5).collect {|x| x**2} ==> [1, 4, 9, 16, 25]
(1..5).create_stream.collect {|x| x**2}.to_a ==> [1, 4, 9, 16, 25]

Instance Attribute Summary

Attributes inherited from WrappedStream

#wrapped_stream

Instance Method Summary collapse

Methods inherited from WrappedStream

#at_beginning?, #at_end?, #set_to_begin, #set_to_end, #unwrapped

Methods included from Stream

#+, #at_beginning?, #at_end?, #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, #set_to_begin, #set_to_end, #unwrapped

Methods included from Enumerable

#create_stream

Constructor Details

#initialize(otherStream, &mapping) ⇒ MappedStream

Creates a new MappedStream wrapping otherStream which calls the block mapping on each move.


345
346
347
348
# File 'lib/stream.rb', line 345

def initialize (otherStream, &mapping)
  super otherStream
  @mapping = mapping
end

Instance Method Details

#basic_backwardObject

Apply the stored closure for the previous element in the wrapped stream and return the result.


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

def basic_backward; @mapping.call(super); end

#basic_forwardObject

Apply the stored closure for the next element in the wrapped stream and return the result.


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

def basic_forward; @mapping.call(super); end