module Sink
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.
Public Instance Methods
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
.
# 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
May be overriden by classes using the Sink
mixin. The default implementation just returns self.
# File lib/coroutines/sink.rb, line 9 def close self end
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
.
# 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
Returns a new sink which supplies each of its input values to the given block and feeds each result of the block to sink
.
# File lib/coroutines/sink.rb, line 40 def input_map(&block) InputMapWrapper.new(self, &block) end
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.
# File lib/coroutines/sink.rb, line 68 def input_reduce(*args, &block) InputReduceWrapper.new(self, *args, &block) end
Returns a new sink which feeds those inputs for which block
returns a false value to sink
and discards all others.
# File lib/coroutines/sink.rb, line 58 def input_reject(&block) InputRejectWrapper.new(self, &block) end
Returns a new sink which feeds those inputs for which block
returns a true value to sink
and discards all others.
# File lib/coroutines/sink.rb, line 49 def input_select(&block) InputSelectWrapper.new(self, &block) end