In Files

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

Enumerable::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 28
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 75
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 86
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 124
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 67
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 43
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 59
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 50
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 100
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 114
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 135
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
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.