Class: Enumerator::Lazy

### Home Classes Methods

### In Files

- lib/backports/2.0.0/enumerable/lazy.rb

### Parent

Enumerator

### Methods

- ::new
- #__set_inspect
- #chunk
- #collect
- #collect_concat
- #drop
- #drop_while
- #enum_for
- #find_all
- #flat_map
- #grep
- #inspect
- #lazy
- #map
- #reject
- #select
- #take
- #take_while
- #to_enum
- #zip

### Class/Module Index

Quicksearch

- Prime
- Prime::EratosthenesGenerator
- Prime::EratosthenesSieve
- Prime::Generator23
- Prime::OldCompatibility
- Prime::PseudoPrimeGenerator
- Prime::TrialDivision
- Prime::TrialDivisionGenerator
- Backports
- Backports::MOST_EXTREME_OBJECT_EVER
- Backports::StdLib
- Backports::StdLib::LoadedFeatures
- Enumerator
- Enumerator::GeneratorBP
- Enumerator::Lazy
- Enumerator::Yielder
- Matrix
- Matrix::EigenvalueDecomposition
- Matrix::LUPDecomposition
- Random
- Random::Implementation
- Random::MT19937
- Enumerable
- Enumerable::Enumerator
- File
- File::Constants
- ARGF
- Array
- BasicObject
- Binding
- Complex
- Dir
- ENV
- Fixnum
- Float
- GC
- Hash
- IO
- Integer
- Kernel
- Math
- Method
- Module
- NilClass
- Numeric
- Object
- ObjectSpace
- OpenStruct
- Proc
- Process
- Range
- Regexp
- Set
- StopIteration
- String
- Struct
- Symbol
- UnboundMethod

No matching classes.

# Enumerator::Lazy

### Public Class Methods

new(obj) click to toggle source

    # File lib/backports/2.0.0/enumerable/lazy.rb, line 26
    def initialize(obj)
      if obj.is_a?(@@lazy_with_no_block)
        @inspect_info = obj
        return super(@receiver = obj.object, @method = obj.method || :each, * @args = obj.args)
      end
      raise ArgumentError, "must supply a block" unless block_given?
      @receiver = obj
      super() do |yielder, *args|
        catch @@done do
          obj.each(*args) do |*x|
            yield yielder, *x
          end
        end
      end
    end

### Public Instance Methods

chunk(*) click to toggle source

    # File lib/backports/2.0.0/enumerable/lazy.rb, line 80
    def chunk(*)
      super.lazy
    end

collect() click to toggle source
Alias for: map
collect_concat() click to toggle source
Alias for: flat_map
drop(n) click to toggle source

    # File lib/backports/2.0.0/enumerable/lazy.rb, line 124
    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.__set_inspect :drop, [n]
    end

drop_while() click to toggle source

    # File lib/backports/2.0.0/enumerable/lazy.rb, line 136
    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.__set_inspect :drop_while
    end

enum_for(method = :each, *args) click to toggle source
Alias for: to_enum
find_all() click to toggle source
Alias for: select
flat_map() click to toggle source

    # File lib/backports/2.0.0/enumerable/lazy.rb, line 162
    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)
        ary = Backports.is_array?(result)
        if ary || (result.respond_to?(:each) && result.respond_to?(:force))
          (ary || result).each{|x| yielder << x }
        else
          yielder << result
        end
      end.__set_inspect :flat_map
    end

Also aliased as: collect_concat
grep(pattern) click to toggle source

    # File lib/backports/2.0.0/enumerable/lazy.rb, line 109
    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.__set_inspect :grep, [pattern]
    end

inspect() click to toggle source

    # File lib/backports/2.0.0/enumerable/lazy.rb, line 53
    def inspect
      suff = ''
      suff << ":#{@method}" unless @method.nil? || @method == :each
      suff << "(#{@args.inspect[1...-1]})" if @args && !@args.empty?
      "#<#{self.class}: #{@receiver.inspect}#{suff}>"
    end

lazy() click to toggle source

    # File lib/backports/2.0.0/enumerable/lazy.rb, line 44
    def lazy
      self
    end

map() click to toggle source

    # File lib/backports/2.0.0/enumerable/lazy.rb, line 84
    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.__set_inspect :map
    end

Also aliased as: collect
reject() click to toggle source

    # File lib/backports/2.0.0/enumerable/lazy.rb, line 101
    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.__set_inspect :reject
    end

select() click to toggle source

    # File lib/backports/2.0.0/enumerable/lazy.rb, line 92
    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.__set_inspect :select
    end

Also aliased as: find_all
take(n) click to toggle source

    # File lib/backports/2.0.0/enumerable/lazy.rb, line 144
    def take(n)
      n = Backports::coerce_to_int(n)
      raise ArgumentError, 'attempt to take negative size' if n < 0
      Lazy.new(n == 0 ? [] : self) do |yielder, *values|
        data = yielder.backports_memo ||= {:remain => n}
        yielder.yield(*values)
        throw @@done if (data[:remain] -= 1) == 0
      end.__set_inspect :take, [n], self
    end

take_while() click to toggle source

    # File lib/backports/2.0.0/enumerable/lazy.rb, line 154
    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.__set_inspect :take_while
    end

to_enum(method = :each, *args) click to toggle source

    # File lib/backports/2.0.0/enumerable/lazy.rb, line 48
    def to_enum(method = :each, *args)
      Lazy.new(@@lazy_with_no_block.new(self, method, args))
    end

Also aliased as: enum_for
zip(*args) click to toggle source

    # File lib/backports/2.0.0/enumerable/lazy.rb, line 176
    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
        args.each do |a|
          raise TypeError, "wrong argument type #{a.class} (must respond to :each)" unless a.respond_to? :each
        end
        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.__set_inspect :zip, args
    end

### Protected Instance Methods

__set_inspect(method, args = nil, receiver = nil) click to toggle source

    # File lib/backports/2.0.0/enumerable/lazy.rb, line 208
    def __set_inspect(method, args = nil, receiver = nil)
      @method = method
      @args = args
      @receiver = receiver if receiver
      self
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.