class Talgene::System(T)
- Talgene::System(T)
- Reference
- Object
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
- Iterator(T)
Defined in:
models/system.crConstructors
-
.new(initial current : T, *, max_advances : Int32 | Nil = nil, include_first at_start : Bool = false)
Creates a new
Talgene::Systeminstance from an initial value and given options. -
.new(initial : T, **options, &)
Creates a new
Talgene::Systeminstance and yields with that newly created instance as the default receiver for method calls within the block.
Instance Method Summary
-
#advances : Int32
Returns the number of advances made in the iteration.
-
#current : T
Returns the current element of the iteration.
-
#next
Returns the next element in this iterator, or
Iterator::Stop::INSTANCEif there are no more elements. -
#previous : T | Nil
Returns the previous element of the iteration or
nilif currently on the first iteration. -
#stop_on(&block : T, T | Nil, Int32 -> Bool) : Nil
Defines an early stop condition that will be checked at every iteration step until
max_advancesis reached.
Constructor Detail
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.
Creates a new Talgene::System instance and yields with that newly created instance
as the default receiver for method calls within the block.
Instance Method Detail
Returns the next element in this iterator, or Iterator::Stop::INSTANCE if there
are no more elements.
Returns the previous element of the iteration or nil if currently on the first
iteration.
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