Module: Sink
- Included in:
- Array, Consumer, Hash, IO, Method, InputReduceWrapper, InputWrapper, Multicast, String
- Defined in:
- lib/coroutines/sink.rb,
lib/coroutines/operators.rb
Overview
The Sink mixin provides classes that can accept streams of values with several utility methods. Classes using this mixin must at least provide an operator << which accepts a value and returns the sink instance (allowing chaining). Optionally, the class may provide a close method that signals end of input and (also optionally) retrives a result value.
Defined Under Namespace
Classes: InputMapWrapper, InputReduceWrapper, InputRejectWrapper, InputSelectWrapper, InputWrapper, Multicast
Instance Method Summary collapse
-
#<=(other) ⇒ Object
:call-seq: sink <= enum -> obj sink <= trans -> new_consum.
-
#close ⇒ Object
May be overriden by classes using the Sink mixin.
-
#in_connect(other) ⇒ Object
:call-seq: sink.in_connect(enum) -> obj sink.in_connect(trans) -> new_consum.
-
#input_map(&block) ⇒ Object
:call-seq: sink.input_map{ |obj| block } -> new_sink.
-
#input_reduce(*args, &block) ⇒ Object
:call-seq: sink.input_reduce(initial=nil){ |memo, obj| block } -> new_sink.
-
#input_reject(&block) ⇒ Object
:call-seq: sink.input_reject{ |obj| block } -> new_sink.
-
#input_select(&block) ⇒ Object
:call-seq: sink.input_select{ |obj| block } -> new_sink.
Instance Method Details
#<=(other) ⇒ Object
:call-seq:
sink <= enum -> obj
sink <= trans -> new_consum
In the first form, iterate over enum and write each result to sink using <<; then return the result of sink.close.
In the second form, create a new Consumer by connecting the output of trans (which must be convertible to a Transformer using the to_trans method) to the input of sink.
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/coroutines/operators.rb', line 38 def <=(other) if other.respond_to? :each begin other.each { |x| self << x } rescue StopIteration end close elsif other.respond_to? :to_trans other >= self end end |
#close ⇒ Object
May be overriden by classes using the Sink mixin. The default implementation just returns self.
9 10 11 |
# File 'lib/coroutines/sink.rb', line 9 def close self end |
#in_connect(other) ⇒ Object
:call-seq:
sink.in_connect(enum) -> obj
sink.in_connect(trans) -> new_consum
In the first form, iterate over enum and write each result to sink using <<; then return the result of sink.close.
In the second form, create a new Consumer by connecting the output of trans (which must be convertible to a Transformer using the to_trans method) to sink.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/coroutines/sink.rb', line 23 def in_connect(other) if other.respond_to? :each begin other.each { |x| self << x } rescue StopIteration end close elsif other.respond_to? :to_trans other.to_trans.out_connect(self) end end |
#input_map(&block) ⇒ Object
:call-seq:
sink.input_map{ |obj| block } -> new_sink
Returns a new sink which supplies each of its input values to the given block and feeds each result of the block to sink.
40 41 42 |
# File 'lib/coroutines/sink.rb', line 40 def input_map(&block) InputMapWrapper.new(self, &block) end |
#input_reduce(*args, &block) ⇒ Object
:call-seq:
sink.input_reduce(initial=nil){ |memo, obj| block } -> new_sink
Returns a new sink which reduces its input values to a single value, as in Enumerable#reduce. When new_sink is closed, the reduced value is fed to sink and sink is closed as well.
68 69 70 |
# File 'lib/coroutines/sink.rb', line 68 def input_reduce(*args, &block) InputReduceWrapper.new(self, *args, &block) end |
#input_reject(&block) ⇒ Object
:call-seq:
sink.input_reject{ |obj| block } -> new_sink
Returns a new sink which feeds those inputs for which block returns a false value to sink and discards all others.
58 59 60 |
# File 'lib/coroutines/sink.rb', line 58 def input_reject(&block) InputRejectWrapper.new(self, &block) end |
#input_select(&block) ⇒ Object
:call-seq:
sink.input_select{ |obj| block } -> new_sink
Returns a new sink which feeds those inputs for which block returns a true value to sink and discards all others.
49 50 51 |
# File 'lib/coroutines/sink.rb', line 49 def input_select(&block) InputSelectWrapper.new(self, &block) end |