This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/iterator/iterator.go
2023-01-10 11:51:46 -05:00

31 lines
618 B
Go

package iterator
type Iterator[ELEMENT_TYPE any] struct {
index int
slice []ELEMENT_TYPE
}
func New[ELEMENT_TYPE any] (
slice []ELEMENT_TYPE,
) (
iterator Iterator[ELEMENT_TYPE],
) {
iterator.slice = slice
return
}
func (iterator *Iterator[ELEMENT_TYPE]) Length () (length int) {
return len(iterator.slice)
}
func (iterator *Iterator[ELEMENT_TYPE]) Next () (element ELEMENT_TYPE) {
if !iterator.MoreLeft() { return }
element = iterator.slice[iterator.index]
iterator.index ++
return
}
func (iterator *Iterator[ELEMENT_TYPE]) MoreLeft () (moreLeft bool) {
return iterator.index < len(iterator.slice)
}