Parent

Enumerator::Lazy

Public Class Methods

new(obj) click to toggle source
# File lib/backports/2.0.0/enumerator/lazy.rb, line 17
def initialize(obj)
  return super(obj.object, :non_lazy_cycle, obj.n) if obj.is_a?(@@cycler)
  raise ArgumentError, "must supply a block" unless block_given?
  super() do |yielder, *args|
    catch @@done do
      obj.each(*args) do |*x|
        yield yielder, *x
      end
    end
  end
end

Public Instance Methods

collect() click to toggle source
Alias for: map
collect_concat() click to toggle source
Alias for: flat_map
cycle(n = nil) click to toggle source
# File lib/backports/2.0.0/enumerator/lazy.rb, line 127
def cycle(n = nil)
  return super if block_given?
  Lazy.new(@@cycler.new(self, n))
end
Also aliased as: non_lazy_cycle
drop(n) click to toggle source
# File lib/backports/2.0.0/enumerator/lazy.rb, line 75
def drop(n)
  n = Backports::coerce_to_int(n)
  Lazy.new(self) do |yielder, *values|
    data = yielder.backports_memo ||= {remain: n}
    if data[:remain] > 0
      data[:remain] -= 1
    else
      yielder.yield(*values)
    end
  end
end
drop_while() click to toggle source
# File lib/backports/2.0.0/enumerator/lazy.rb, line 87
def drop_while
  raise ArgumentError, "tried to call lazy drop_while without a block" unless block_given?
  Lazy.new(self) do |yielder, *values|
    data = yielder.backports_memo ||= {dropping: true}
    yielder.yield(*values) unless data[:dropping] &&= yield(*values)
  end
end
find_all() click to toggle source
Alias for: select
flat_map() click to toggle source
# File lib/backports/2.0.0/enumerator/lazy.rb, line 114
def flat_map
  raise ArgumentError, "tried to call lazy flat_map without a block" unless block_given?
  Lazy.new(self) do |yielder, *values|
    result = yield(*values)
    if ary = Backports.is_array?(result) || (result.respond_to?(:each) && result.respond_to?(:force))
      ary.each{|x| yielder << x }
    else
      yielder << result
    end
  end
end
Also aliased as: collect_concat
grep(pattern) click to toggle source
# File lib/backports/2.0.0/enumerator/lazy.rb, line 60
def grep(pattern)
  if block_given?
    # Split for performance
    Lazy.new(self) do |yielder, *values|
      values = values.first unless values.size > 1
      yielder.yield(yield(values)) if pattern === values
    end
  else
    Lazy.new(self) do |yielder, *values|
      values = values.first unless values.size > 1
      yielder.yield(values) if pattern === values
    end
  end
end
lazy() click to toggle source
# File lib/backports/2.0.0/enumerator/lazy.rb, line 31
def lazy
  self
end
map() click to toggle source
# File lib/backports/2.0.0/enumerator/lazy.rb, line 35
def map
  raise ArgumentError, "tried to call lazy map without a block" unless block_given?
  Lazy.new(self) do |yielder, *values|
    yielder << yield(*values)
  end
end
Also aliased as: collect
non_lazy_cycle(n = nil) click to toggle source
Alias for: cycle
reject() click to toggle source
# File lib/backports/2.0.0/enumerator/lazy.rb, line 52
def reject
  raise ArgumentError, "tried to call lazy reject without a block" unless block_given?
  Lazy.new(self) do |yielder, *values|
    values = values.first unless values.size > 1
    yielder.yield(values) unless yield values
  end
end
select() click to toggle source
# File lib/backports/2.0.0/enumerator/lazy.rb, line 43
def select
  raise ArgumentError, "tried to call lazy select without a block" unless block_given?
  Lazy.new(self) do |yielder, *values|
    values = values.first unless values.size > 1
    yielder.yield values if yield values
  end
end
Also aliased as: find_all
take(n) click to toggle source
# File lib/backports/2.0.0/enumerator/lazy.rb, line 95
def take(n)
  n = Backports::coerce_to_int(n)
  raise ArgumentError, 'attempt to take negative size' if n < 0
  return Lazy.new([]){} if n == 0
  Lazy.new(self) do |yielder, *values|
    data = yielder.backports_memo ||= {remain: n}
    yielder.yield(*values)
    throw @@done if (data[:remain] -= 1) == 0
  end
end
take_while() click to toggle source
# File lib/backports/2.0.0/enumerator/lazy.rb, line 106
def take_while
  raise ArgumentError, "tried to call lazy take_while without a block" unless block_given?
  Lazy.new(self) do |yielder, *values|
    throw @@done unless yield(*values)
    yielder.yield(*values)
  end
end
zip(*args) click to toggle source
# File lib/backports/2.0.0/enumerator/lazy.rb, line 132
def zip(*args)
  return super if block_given?
  arys = args.map{ |arg| Backports.is_array?(arg) }
  if arys.all?
    # Handle trivial case of multiple array arguments separately
    # by avoiding Enumerator#next for efficiency & compatibility
    Lazy.new(self) do |yielder, *values|
      data = yielder.backports_memo ||= {iter: 0}
      values = values.first unless values.size > 1
      yielder << arys.map{|ary| ary[data[:iter]]}.unshift(values)
      data[:iter] += 1
    end
  else
    Lazy.new(self) do |yielder, *values|
      enums = yielder.backports_memo ||= args.map(&:to_enum)
      values = values.first unless values.size > 1
      others = enums.map do |arg|
        begin
          arg.next
        rescue StopIteration
          nil
        end
      end
      yielder << others.unshift(values)
    end
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.