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 Method Summary collapse

Constructor Details

#initialize(other_stream, &mapping) ⇒ MappedStream

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



469
470
471
472
# File 'lib/stream.rb', line 469

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

Instance Method Details

#basic_backwardObject

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



482
483
484
# File 'lib/stream.rb', line 482

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.



476
477
478
# File 'lib/stream.rb', line 476

def basic_forward
  @mapping.call(super)
end