Module: Enumerable
- Defined in:
- lib/coroutines.rb,
lib/coroutines/operators.rb
Overview
– Most of the Sink methods mirror Enumerable, except for #in_connect for symmetry, also add an #out_connect method to Enumerable ++
Instance Method Summary collapse
-
#>=(other) ⇒ Object
:call-seq: enum >= sink -> obj enum >= trans -> new_enum.
-
#filter_map(&block) ⇒ Object
:call-seq: enum.filter_map {|obj| block } -> array.
-
#out_connect(other) ⇒ Object
:call-seq: enum.out_connect(sink) -> obj enum.out_connect(trans) -> new_enum.
Instance Method Details
#>=(other) ⇒ Object
:call-seq:
enum >= sink -> obj
enum >= trans -> new_enum
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 Enumerator by connecting the output of enum to the input of trans (which must be convertible to a Transformer using the to_trans method).
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/coroutines/operators.rb', line 14 def >=(other) if other.respond_to? :<< begin each { |x| other << x } rescue StopIteration end other.close elsif other.respond_to? :to_trans other <= self end end |
#filter_map(&block) ⇒ Object
:call-seq:
enum.filter_map {|obj| block } -> array
For each obj in in enum, calls block, and collects its non-nil return values into a new array. – taken from the API doc of Enumerator::Lazy.new
38 39 40 |
# File 'lib/coroutines.rb', line 38 def filter_map(&block) map(&block).compact end |
#out_connect(other) ⇒ Object
:call-seq:
enum.out_connect(sink) -> obj
enum.out_connect(trans) -> new_enum
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 Enumerator by connecting the output of enum to the input of trans (which must be convertible to a Transformer using the to_trans method).
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/coroutines.rb', line 19 def out_connect(other) if other.respond_to? :<< begin each { |x| other << x } rescue StopIteration end other.close elsif other.respond_to? :to_trans other.to_trans.in_connect(self) end end |