From de6535a2739d7df13c1808be868b0261c6059213 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Mon, 17 Jul 2023 19:22:47 +0200 Subject: [PATCH] put iterator in separate source file; rename tests (unit->lib) --- lib/funky/funky.go | 31 ----------------------------- lib/funky/iterator.go | 30 ++++++++++++++++++++++++++++ tests/{unit_test.go => lib_test.go} | 2 +- 3 files changed, 31 insertions(+), 32 deletions(-) create mode 100644 lib/funky/iterator.go rename tests/{unit_test.go => lib_test.go} (98%) diff --git a/lib/funky/funky.go b/lib/funky/funky.go index 3b6d385..435b12b 100644 --- a/lib/funky/funky.go +++ b/lib/funky/funky.go @@ -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 -} diff --git a/lib/funky/iterator.go b/lib/funky/iterator.go new file mode 100644 index 0000000..39b30ad --- /dev/null +++ b/lib/funky/iterator.go @@ -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 +} diff --git a/tests/unit_test.go b/tests/lib_test.go similarity index 98% rename from tests/unit_test.go rename to tests/lib_test.go index 9172ec3..2b2a137 100644 --- a/tests/unit_test.go +++ b/tests/lib_test.go @@ -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)