iterator simplifications
This commit is contained in:
parent
9c9992065b
commit
3a5225e0d6
1 changed files with 3 additions and 13 deletions
|
@ -33,7 +33,6 @@ func (m *maybe[V]) Value() V {
|
||||||
// Iterator
|
// Iterator
|
||||||
|
|
||||||
type Iterator[V any] interface {
|
type Iterator[V any] interface {
|
||||||
Maybe[V]
|
|
||||||
Next() Maybe[V]
|
Next() Maybe[V]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,24 +42,15 @@ type sliceIt[V any] struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func SliceIterator[V any](sl []V) Iterator[V] {
|
func SliceIterator[V any](sl []V) Iterator[V] {
|
||||||
return &sliceIt[V]{slice: sl}
|
return &sliceIt[V]{-1, sl}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it *sliceIt[V]) Next() Maybe[V] {
|
func (it *sliceIt[V]) Next() Maybe[V] {
|
||||||
if it.IsNothing() {
|
if it.idx >= len(it.slice)-1 {
|
||||||
return Nothing[V]()
|
return Nothing[V]()
|
||||||
}
|
}
|
||||||
v := it.Value()
|
|
||||||
it.idx++
|
it.idx++
|
||||||
return Just(v)
|
return Just(it.slice[it.idx])
|
||||||
}
|
|
||||||
|
|
||||||
func (it *sliceIt[V]) IsNothing() bool {
|
|
||||||
return it.idx >= len(it.slice)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (it *sliceIt[V]) Value() V {
|
|
||||||
return it.slice[it.idx]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Slice[V any](it Iterator[V]) []V {
|
func Slice[V any](it Iterator[V]) []V {
|
||||||
|
|
Loading…
Add table
Reference in a new issue