Method: Proc#to_trans
- Defined in:
- lib/coroutines/operators.rb
#to_trans ⇒ Object
:call-seq:
proc.to_trans -> transformer
Allows implicit conversion of Proc to Transformer. The transformer is a combination of map and filter over its input values: For each input value, proc is called with the input value as parameter. Every non-nil value returned by proc is yielded as an output value.
This is similar to Enumerable#map followed by Array#compact, but without constructing the intermediate Array (so it’s even more similar to something like enum.lazy.map(&proc).reject(&:nil?), using the lazy enumerator introduced in Ruby 2.0).
Example:
(1..10) >= proc{|x| x.to_s + ", " if x.even? } >= ""
# => "2, 4, 6, 8, 10, "
119 120 121 122 123 124 125 126 |
# File 'lib/coroutines/operators.rb', line 119 def to_trans Transformer.new do |y| loop do value = self.call(y.await) y.yield value unless value.nil? end end end |