In Files

  • mrbgems/mruby-enum-lazy/mrblib/lazy.rb

Enumerator::Lazy

Acknowledgements

Based on https://github.com/yhara/enumerable-lazy
Inspired by https://github.com/antimon2/enumerable_lz
http://jp.rubyist.net/magazine/?0034-Enumerable_lz (ja)

Public Class Methods

new(obj, &block) click to toggle source
 
               # File mrbgems/mruby-enum-lazy/mrblib/lazy.rb, line 30
def initialize(obj, &block)
  super(){|yielder|
    begin
      obj.each{|x|
        if block
          block.call(yielder, x)
        else
          yielder << x
        end
      }
    rescue StopIteration
    end
  }
end
            

Public Instance Methods

collect(&block) click to toggle source
Alias for: map
collect_concat(&block) click to toggle source
Alias for: flat_map
drop(n) click to toggle source
 
               # File mrbgems/mruby-enum-lazy/mrblib/lazy.rb, line 89
def drop(n)
  dropped = 0
  Lazy.new(self){|yielder, val|
    if dropped < n
      dropped += 1
    else
      yielder << val
    end
  }
end
            
drop_while(&block) click to toggle source
 
               # File mrbgems/mruby-enum-lazy/mrblib/lazy.rb, line 100
def drop_while(&block)
  dropping = true
  Lazy.new(self){|yielder, val|
    if dropping
      if not block.call(val)
        yielder << val
        dropping = false
      end
    else
      yielder << val
    end
  }
end
            
enum_for(meth=:each, *args, &block) click to toggle source
Alias for: to_enum
find_all(&block) click to toggle source
Alias for: select
flat_map(&block) click to toggle source
 
               # File mrbgems/mruby-enum-lazy/mrblib/lazy.rb, line 138
def flat_map(&block)
  Lazy.new(self){|yielder, val|
    ary = block.call(val)
    # TODO: check ary is an Array
    ary.each{|x|
      yielder << x
    }
  }
end
            
Also aliased as: collect_concat
grep(pattern) click to toggle source
 
               # File mrbgems/mruby-enum-lazy/mrblib/lazy.rb, line 81
def grep(pattern)
  Lazy.new(self){|yielder, val|
    if pattern === val
      yielder << val
    end
  }
end
            
map(&block) click to toggle source
 
               # File mrbgems/mruby-enum-lazy/mrblib/lazy.rb, line 57
def map(&block)
  Lazy.new(self){|yielder, val|
    yielder << block.call(val)
  }
end
            
Also aliased as: collect
reject(&block) click to toggle source
 
               # File mrbgems/mruby-enum-lazy/mrblib/lazy.rb, line 73
def reject(&block)
  Lazy.new(self){|yielder, val|
    unless block.call(val)
      yielder << val
    end
  }
end
            
select(&block) click to toggle source
 
               # File mrbgems/mruby-enum-lazy/mrblib/lazy.rb, line 64
def select(&block)
  Lazy.new(self){|yielder, val|
    if block.call(val)
      yielder << val
    end
  }
end
            
Also aliased as: find_all
take(n) click to toggle source
 
               # File mrbgems/mruby-enum-lazy/mrblib/lazy.rb, line 114
def take(n)
  if n == 0
    return Lazy.new(self){raise StopIteration}
  end
  taken = 0
  Lazy.new(self){|yielder, val|
    yielder << val
    taken += 1
    if taken >= n
      raise StopIteration
    end
  }
end
            
take_while(&block) click to toggle source
 
               # File mrbgems/mruby-enum-lazy/mrblib/lazy.rb, line 128
def take_while(&block)
  Lazy.new(self){|yielder, val|
    if block.call(val)
      yielder << val
    else
      raise StopIteration
    end
  }
end
            
to_enum(meth=:each, *args, &block) click to toggle source
 
               # File mrbgems/mruby-enum-lazy/mrblib/lazy.rb, line 45
def to_enum(meth=:each, *args, &block)
  unless self.respond_to?(meth)
    raise ArgumentError, "undefined method #{meth}"
  end
  lz = Lazy.new(self, &block)
  lz.obj = self
  lz.meth = meth
  lz.args = args
  lz
end
            
Also aliased as: enum_for
uniq(&block) click to toggle source
 
               # File mrbgems/mruby-enum-lazy/mrblib/lazy.rb, line 161
def uniq(&block)
  hash = {}
  Lazy.new(self){|yielder, val|
    if block
      v = block.call(val)
    else
      v = val
    end
    unless hash.include?(v)
      yielder << val
      hash[v] = val
    end
  }
end
            
zip(*args, &block) click to toggle source
 
               # File mrbgems/mruby-enum-lazy/mrblib/lazy.rb, line 149
def zip(*args, &block)
  enums = [self] + args
  Lazy.new(self){|yielder, val|
    ary = enums.map{|e| e.next}
    if block
      yielder << block.call(ary)
    else
      yielder << ary
    end
  }
end