put iterator in separate source file; rename tests (unit->lib)
This commit is contained in:
parent
3a5225e0d6
commit
de6535a273
3 changed files with 31 additions and 32 deletions
|
@ -29,34 +29,3 @@ func (m *maybe[V]) IsNothing() bool {
|
|||
func (m *maybe[V]) Value() V {
|
||||
return m.value
|
||||
}
|
||||
|
||||
// Iterator
|
||||
|
||||
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
|
||||
}
|
||||
|
|
30
lib/funky/iterator.go
Normal file
30
lib/funky/iterator.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
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
|
||||
}
|
|
@ -13,7 +13,7 @@ import (
|
|||
"git.sr.ht/~cco/go-scopes/testing"
|
||||
)
|
||||
|
||||
func TestUnit(tb *tbase.T) {
|
||||
func TestLib(tb *tbase.T) {
|
||||
t := testing.SetUp(tb)
|
||||
t.Run("address", AddressTest)
|
||||
t.Run("message", MessageTest)
|
Loading…
Add table
Reference in a new issue