SimpleIterator
Documentation for SimpleIterator.jl.
SimpleIterator.jl is a Julia package that provides a simple Python-like iterator interface for Julia. It enables the iteration of an object x
becomes iteration over iterator(x)
. The iteration behavior can be easily customized by overloading the iterator
function and returning the desired iterator object.
SimpleIterator.@iterfn
— Macro@iterfn
Define a iterator for a type. The type should have a function named iterator
which returns a iterator. After the macro is called, iteration over the type will automatically becomes iteration over the iterator.
Arguments
fundef
: function definition to be decorated, the function name should beiterator
.
Example
struct MyType
data::Vector{Int}
end
@iterfn iterator(x::MyType) = x.data
for i in MyType([1, 2, 3])
println(i)
end
SimpleIterator.iterator
— Functioniterator(x)
Overload this function to define the iterator for a type. The function should return a iterator. This function MUST be overloaded with the @iterfn
macro.
SimpleIterator.IteratorType
— FunctionIteratorType(::Type{T})
Return the iterator type of a type. Overload this function to define the iterator type of a type. The function should return a type. The default implementation is Any
. Overload this function to enable iteration utilities related to the type such as Base.length
and Base.eltype
. If the iterator
function is defined with return type annotation, this function will be automatically overloaded.