collect(&block)
click to toggle source
collect_concat(&block)
click to toggle source
drop(n)
click to toggle source
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
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
find_all(&block)
click to toggle source
flat_map(&block)
click to toggle source
def flat_map(&block)
Lazy.new(self){|yielder, val|
ary = block.call(val)
ary.each{|x|
yielder << x
}
}
end
grep(pattern)
click to toggle source
def grep(pattern)
Lazy.new(self){|yielder, val|
if pattern === val
yielder << val
end
}
end
map(&block)
click to toggle source
def map(&block)
Lazy.new(self){|yielder, val|
yielder << block.call(val)
}
end
reject(&block)
click to toggle source
def reject(&block)
Lazy.new(self){|yielder, val|
unless block.call(val)
yielder << val
end
}
end
select(&block)
click to toggle source
def select(&block)
Lazy.new(self){|yielder, val|
if block.call(val)
yielder << val
end
}
end
take(n)
click to toggle source
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
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
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
uniq(&block)
click to toggle source
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
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