class Iter

A stable iterator class. (You can reorder/remove elements in the container without affecting iteration.)

For example, to reverse all the elements in a list:

>> i = Iter.new(1..10)
>> i.each_cons(2) { |a,b| b.move_before(a) }
>> i.to_a    #=> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Attributes

container[RW]

Public Class Methods

from_elems(elems) click to toggle source
# File lib/epitools/iter.rb, line 24
def self.from_elems(elems)
  new([]).tap { |i| i.container = elems }
end
new(vals) click to toggle source
# File lib/epitools/iter.rb, line 15
def initialize(vals)
  @container = vals.map { |val| elem(val) }
end

Public Instance Methods

==(other) click to toggle source
# File lib/epitools/iter.rb, line 28
def ==(other)
  case other
  when Iter
    @container == other.container
  when Array
    @container == other
  end
end
each() { |elem| ... } click to toggle source
# File lib/epitools/iter.rb, line 37
def each
  @container.each do |elem|
    yield elem
    elem.visited = true
  end
end
each_cons(num=1) { |*elems| ... } click to toggle source
# File lib/epitools/iter.rb, line 44
def each_cons(num=1)
  @container.each_cons(num) do |(*elems)|
    yield *elems
    elems.each { |e| e.visited = true }
  end
end
Also aliased as: iterate, every
elem(val) click to toggle source

Wrap a value in an Elem container

# File lib/epitools/iter.rb, line 20
def elem(val)
  Elem.new(self, val)
end
every(num=1)
Alias for: each_cons
iterate(num=1)
Alias for: each_cons
method_missing(name, *args) click to toggle source
# File lib/epitools/iter.rb, line 58
def method_missing(name, *args)
  result = @container.send(name, *args)
  case result
  when Array
    Iter.from_elems result
  else
    result
  end
end
to_a() click to toggle source
# File lib/epitools/iter.rb, line 54
def to_a
  @container.map(&:val)
end