30 lines
496 B
Go
30 lines
496 B
Go
package funky
|
|
|
|
type Iterator[V any] interface {
|
|
Next() Maybe[V]
|
|
}
|
|
|
|
type sliceIt[V any] struct {
|
|
idx int
|
|
slice []V
|
|
}
|
|
|
|
func SliceIterator[V any](sl []V) Iterator[V] {
|
|
return &sliceIt[V]{-1, sl}
|
|
}
|
|
|
|
func (it *sliceIt[V]) Next() Maybe[V] {
|
|
if it.idx >= len(it.slice)-1 {
|
|
return Nothing[V]()
|
|
}
|
|
it.idx++
|
|
return Just(it.slice[it.idx])
|
|
}
|
|
|
|
func Slice[V any](it Iterator[V]) []V {
|
|
var out []V
|
|
for m := it.Next(); !m.IsNothing(); m = it.Next() {
|
|
out = append(out, m.Value())
|
|
}
|
|
return out
|
|
}
|