Skip to content

Commit

Permalink
Rename iterator struct to Iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
hackirby committed Oct 23, 2023
1 parent 7913df6 commit f96ace7
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions itertools.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,38 @@ func slice(v any) bool {
return reflect.TypeOf(v).Kind() == reflect.Slice
}

type iterator struct {
type Iterator struct {
items any
index int
cycle bool
}

// Iter creates a new iterator from any slice
func Iter(items any) (*iterator, error) {
// Iter creates a new Iterator from any slice
func Iter(items any) (*Iterator, error) {
if !slice(items) {
return nil, errors.New("Iter() requires a slice")
}
return &iterator{items, -1, false}, nil
return &Iterator{items, -1, false}, nil
}

// Cycle creates an infinite iterator from any slice
func Cycle(items any) (*iterator, error) {
// Cycle creates an infinite Iterator from any slice
func Cycle(items any) (*Iterator, error) {
if !slice(items) {
return nil, errors.New("Cycle() requires a slice")
}
return &iterator{items, -1, true}, nil
return &Iterator{items, -1, true}, nil
}

// HasNext returns true if the iterator has a next item (always true if cycle mode is enabled)
func (it *iterator) HasNext() bool {
// HasNext returns true if the Iterator has a next item (always true if cycle mode is enabled)
func (it *Iterator) HasNext() bool {
if it.cycle {
return true
}
return it.index+1 < reflect.ValueOf(it.items).Len()
}

// Next returns the next item (nil if not set)
func (it *iterator) Next() any {
func (it *Iterator) Next() any {
val := reflect.ValueOf(it.items)
if it.cycle {
it.index = (it.index + 1) % val.Len()
Expand All @@ -53,16 +53,16 @@ func (it *iterator) Next() any {
return nil
}

// HasPrev returns true if the iterator has a previous item (always true if cycle mode is enabled)
func (it *iterator) HasPrev() bool {
// HasPrev returns true if the Iterator has a previous item (always true if cycle mode is enabled)
func (it *Iterator) HasPrev() bool {
if it.cycle {
return true
}
return it.index-1 >= 0
}

// Prev returns the previous item (nil if not set)
func (it *iterator) Prev() any {
func (it *Iterator) Prev() any {
val := reflect.ValueOf(it.items)
if it.index-1 >= 0 {
it.index--
Expand All @@ -76,36 +76,36 @@ func (it *iterator) Prev() any {
}

// Current returns the current item (nil if not set)
func (it *iterator) Current() any {
func (it *Iterator) Current() any {
val := reflect.ValueOf(it.items)
if it.index >= 0 && it.index < val.Len() {
return val.Index(it.index).Interface()
}
return nil
}

// Len returns the length of the iterator
func (it *iterator) Len() int {
// Len returns the length of the Iterator
func (it *Iterator) Len() int {
return reflect.ValueOf(it.items).Len()
}

// Index returns the current index (0-based)
func (it *iterator) Index() int {
func (it *Iterator) Index() int {
return it.index
}

// IsCycle returns true if the iterator is in cycle mode
func (it *iterator) IsCycle() bool {
// IsCycle returns true if the Iterator is in cycle mode
func (it *Iterator) IsCycle() bool {
return it.cycle
}

// Reset resets the iterator to the beginning (nil item)
func (it *iterator) Reset() {
// Reset resets the Iterator to the beginning (nil item)
func (it *Iterator) Reset() {
it.index = -1
}

// SetIndex sets the iterator to the given index (0-based)
func (it *iterator) SetIndex(index int) error {
// SetIndex sets the Iterator to the given index (0-based)
func (it *Iterator) SetIndex(index int) error {
if it.cycle {
it.index = index % reflect.ValueOf(it.items).Len()
return nil
Expand All @@ -118,7 +118,7 @@ func (it *iterator) SetIndex(index int) error {
return errors.New("Index out of range")
}

// SetCycle sets the iterator to cycle mode (true) or not (false)
func (it *iterator) SetCycle(cycle bool) {
// SetCycle sets the Iterator to cycle mode (true) or not (false)
func (it *Iterator) SetCycle(cycle bool) {
it.cycle = cycle
}

0 comments on commit f96ace7

Please sign in to comment.