class Transformer::Lazy

Public Class Methods

new(trans) click to toggle source
# File lib/coroutines/base.rb, line 246
def initialize(trans)
        @trans = trans.instance_variable_get :@self
end

Public Instance Methods

collect(&block)
Alias for: map
collect_concat(&block)
Alias for: flat_map
count() click to toggle source
# File lib/coroutines/base.rb, line 270
def count
        Consumer.new do |y|
                yy = Yielder.new y
                n = 0
                yy.define_yield do |*values|
                        n += 1
                        yy
                end
                @trans.call yy
                n
        end
end
drop(n) click to toggle source
# File lib/coroutines/base.rb, line 283
def drop(n)
        Transformer.new do |y|
                yy = Yielder.new y
                to_drop = n
                yy.define_yield do |*values|
                        if to_drop > 0
                                to_drop -= 1
                        else
                                y.yield(*values)
                        end
                        yy
                end
                @trans.call yy
        end
end
drop_while(&block) click to toggle source
# File lib/coroutines/base.rb, line 299
def drop_while(&block)
        Transformer.new do |y|
                yy = Yielder.new y
                dropping = true
                yy.define_yield do |*values|
                        if dropping
                                if not block.call(*values)
                                        dropping = false
                                        y.yield(*values)
                                end
                        else
                                y.yield(*values)
                        end
                        yy
                end
                @trans.call yy
        end
end
each(&block) click to toggle source
# File lib/coroutines/base.rb, line 318
def each(&block)
        Consumer.new do |y|
                yy = Yielder.new y
                yy.define_yield do |*values|
                        block.call(*values)
                        yy
                end
                @trans.call yy
        end
end
filter_map(&block) click to toggle source
# File lib/coroutines/base.rb, line 329
def filter_map(&block)
        Transformer.new do |y|
                yy = Yielder.new y
                yy.define_yield do |*values|
                        x = block.call(*values)
                        y.yield(x) unless x.nil?
                        yy
                end
                @trans.call yy
        end
end
flat_map(&block) click to toggle source
# File lib/coroutines/base.rb, line 341
def flat_map(&block)
        Transformer.new do |y|
                yy = Yielder.new y
                yy.define_yield do |*values|
                        x = block.call(*values)
                        if x.respond_to? :to_ary
                                x.to_ary.each{|xx| y.yield xx }
                        else
                                y.yield x
                        end
                        yy
                end
                @trans.call yy
        end
end
Also aliased as: collect_concat
inject(*args)
Alias for: reduce
lazy() click to toggle source
# File lib/coroutines/base.rb, line 250
def lazy
        self
end
map(&block) click to toggle source
# File lib/coroutines/base.rb, line 358
def map(&block)
        Transformer.new do |y|
                yy = Yielder.new y
                yy.define_yield do |*values|
                        y.yield(block.call(*values))
                        yy
                end
                @trans.call yy
        end
end
Also aliased as: collect
out_connect(other) click to toggle source
# File lib/coroutines/base.rb, line 370
def out_connect(other)
        Transformer.new(&@trans).out_connect(other)
end
reduce(*args) click to toggle source
# File lib/coroutines/base.rb, line 374
def reduce(*args)
        if not block_given?
                if args.size == 1
                        return reduce(&args[0].to_proc)
                elsif args.size == 2
                        return reduce(args[0], &args[1].to_proc)
                else
                        raise ArgumentError, "wrong number of arguments"
                end
        end
        raise ArgumentError, "wrong number of arguments" if args.size > 1
        block = proc

        memo = if args.empty? then nil else args[0] end
        Consumer.new do |y|
                yy = Yielder.new y
                yy.define_yield do |value|
                        if memo.nil?
                                memo = value
                        else
                                memo = block.call memo, value
                        end
                        yy
                end
                @trans.call yy
                memo
        end
end
Also aliased as: inject
reject(&block) click to toggle source
# File lib/coroutines/base.rb, line 404
def reject(&block)
        Transformer.new do |y|
                yy = Yielder.new y
                yy.define_yield do |*values|
                        y.yield(*values) unless block.call(*values)
                        yy
                end
                @trans.call yy
        end
end
select(&block) click to toggle source
# File lib/coroutines/base.rb, line 415
def select(&block)
        Transformer.new do |y|
                yy = Yielder.new y
                yy.define_yield do |*values|
                        y.yield(*values) if block.call(*values)
                        yy
                end
                @trans.call yy
        end
end
sort() click to toggle source
# File lib/coroutines/base.rb, line 458
def sort
        Consumer.new do |y|
                yy = Yielder.new y
                result = []
                yy.define_yield do |value|
                        result << value
                        yy
                end
                @trans.call yy
                result.sort
        end
end
sort_by(&block) click to toggle source
# File lib/coroutines/base.rb, line 471
def sort_by(&block)
        Consumer.new do |y|
                yy = Yielder.new y
                result = []
                yy.define_yield do |value|
                        result << value
                        yy
                end
                @trans.call yy
                result.sort_by(&block)
        end
end
take(n) click to toggle source
# File lib/coroutines/base.rb, line 426
def take(n)
        Transformer.new do |y|
                yy = Yielder.new y
                to_take = n
                yy.define_yield do |*values|
                        if to_take > 0
                                y.yield(*values)
                                to_take -= 1
                        else
                                raise StopIteration
                        end
                        yy
                end
                @trans.call yy
        end
end
take_while(&block) click to toggle source
# File lib/coroutines/base.rb, line 443
def take_while(&block)
        Transformer.new do |y|
                yy = Yielder.new y
                yy.define_yield do |*values|
                        if block.call(*values)
                                y.yield(*values)
                        else
                                raise StopIteration
                        end
                        yy
                end
                @trans.call yy
        end
end
to_a() click to toggle source
# File lib/coroutines/base.rb, line 484
def to_a
        Consumer.new do |y|
                yy = Yielder.new y
                result = []
                yy.define_yield do |value|
                        result << value
                        yy
                end
                @trans.call yy
                result
        end
end
to_h() click to toggle source
# File lib/coroutines/base.rb, line 497
def to_h
        Consumer.new do |y|
                yy = Yielder.new y
                result = {}
                yy.define_yield do |value|
                        result[value[0]] = value[1]
                        yy
                end
                @trans.call yy
                result
        end
end