class

Talgene::System(T)

Inherits Iterator < Enumerable < Reference < Object

Talgene::System is an iterator over types that define an #advance method. See Talgene::Advanceable module.

require "talgene"

record Integer, value : Int32 do
  include Talgene::Advanceable(Integer)

  def advance : Integer
    Integer.new @value + 1
  end
end

system = Talgene::System.new Integer.new(0)
system.next # => Integer(@value=1)

# Use `include_first` to include the initial entry
system = Talgene::System.new Integer.new(0), include_first: true
system.next # => Integer(@value=0)

Constructors

new(initial current : T, *, max_advances : Int32 | Nil = nil, include_first at_start : Bool = false)

Creates a new Talgene::System instance from an initial value and given options.

When max_advances is nil the iterator never stops. When include_first is true the initial entry is used as first value of the iteration. This will not affect the number of total advances.

Source
new(initial : T, **options, &)

Creates a new Talgene::System instance and yields with that newly created instance as the default receiver for method calls within the block.

Source

Instance methods

advances

Returns the number of advances made in the iteration.

Source
current

Returns the current element of the iteration.

Source
next

Returns the next element in this iterator, or Iterator::Stop::INSTANCE if there are no more elements.

Source
previous

Returns the previous element of the iteration or nil if currently on the first iteration.

Source
stop_on

Defines an early stop condition that will be checked at every iteration step until max_advances is reached. On condition matched the iterator stops immediately.

It yields the current element, the previous one and the total number of advances made.

require "talgene"

record Integer, value : Int32 do
  include Talgene::Advanceable(Integer)

  def advance : Integer
    Integer.new @value + 1
  end
end

system = Talgene::System.new Integer.new(0)

system.stop_on do |int|
  int.value > 93.0
end

system.size # => 94
Source