class Talgene::System(T)

Overview

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)

Included Modules

Defined in:

models/system.cr

Constructors

Instance Method Summary

Constructor Detail

def self.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.


[View source]
def self.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.


[View source]

Instance Method Detail

def advances : Int32 #

Returns the number of advances made in the iteration.


[View source]
def current : T #

Returns the current element of the iteration.


[View source]
def next #
Description copied from module Iterator(T)

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


[View source]
def previous : T | Nil #

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


[View source]
def stop_on(&block : T, T | Nil, Int32 -> Bool) : Nil #

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

[View source]