In Files

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

Enumerable::Lazy

Public Class Methods

new(obj, &block) click to toggle source
 
               # File mrbgems/mruby-enum-lazy/mrblib/lazy.rb, line 29
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 76
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 87
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
            
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 125
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 68
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 44
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 60
def reject(&block)
  Lazy.new(self){|yielder, val|
    if not block.call(val)
      yielder << val
    end
  }
end
            
select(&block) click to toggle source
 
               # File mrbgems/mruby-enum-lazy/mrblib/lazy.rb, line 51
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 101
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 115
def take_while(&block)
  Lazy.new(self){|yielder, val|
    if block.call(val)
      yielder << val
    else
      raise StopIteration
    end
  }
end
            
zip(*args, &block) click to toggle source
 
               # File mrbgems/mruby-enum-lazy/mrblib/lazy.rb, line 136
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