From 591191deb038a42edac1410eb784c854d6a771da Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Mon, 7 Aug 2023 13:54:50 +0200 Subject: [PATCH] remove obsolete 'funky' package --- common/funky/funky.go | 73 -------------------------------------- common/funky/iterator.go | 30 ---------------- forge/builtins/builtins.go | 2 +- tests/forge_test.go | 1 - tests/funky_test.go | 42 ---------------------- 5 files changed, 1 insertion(+), 147 deletions(-) delete mode 100644 common/funky/funky.go delete mode 100644 common/funky/iterator.go delete mode 100644 tests/funky_test.go diff --git a/common/funky/funky.go b/common/funky/funky.go deleted file mode 100644 index a79099b..0000000 --- a/common/funky/funky.go +++ /dev/null @@ -1,73 +0,0 @@ -// Package funky provides a few constructs usually present in pure -// functional programming environments, using Go 1.18 generics. -package funky - -// Maybe (sometimes called `Option`) - -type Maybe[V any] interface { - IsNothing() bool - Value() V -} - -type maybe[V any] struct { - isNothing bool - value V -} - -func Nothing[V any]() Maybe[V] { - return &maybe[V]{isNothing: true} -} - -func Just[V any](v V) Maybe[V] { - return &maybe[V]{false, v} -} - -func (m *maybe[V]) IsNothing() bool { - return m.isNothing -} - -func (m *maybe[V]) Value() V { - return m.value -} - -// Either (sometimes called `Result`) - -type Either[V any] interface { - Maybe[V] - Ok() bool - Value() V - Error() error -} - -type either[V any] struct { - ok bool - value V - error error -} - -func Value[V any](v V) Either[V] { - return &either[V]{ - ok: true, - value: v, - } -} - -func Error[V any](e error) Either[V] { - return &either[V]{error: e} -} - -func (et *either[V]) Ok() bool { - return et.ok -} - -func (et *either[V]) IsNothing() bool { - return !et.Ok() -} - -func (et *either[V]) Value() V { - return et.value -} - -func (et *either[V]) Error() error { - return et.error -} diff --git a/common/funky/iterator.go b/common/funky/iterator.go deleted file mode 100644 index 39b30ad..0000000 --- a/common/funky/iterator.go +++ /dev/null @@ -1,30 +0,0 @@ -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/forge/builtins/builtins.go b/forge/builtins/builtins.go index f4f24db..1cc61ec 100644 --- a/forge/builtins/builtins.go +++ b/forge/builtins/builtins.go @@ -64,7 +64,7 @@ func setup(f FE) *builtins { } func reg(f FE) { - f.Register(forge.FCode(f.Pop().(string), f.Pop().(FPtr).Reset())) + f.Register(forge.FCode(f.Pop().(string), f.Pop().(FPtr))) } func create(f FE, _ XT) { diff --git a/tests/forge_test.go b/tests/forge_test.go index 2d471e1..e69a96c 100644 --- a/tests/forge_test.go +++ b/tests/forge_test.go @@ -31,7 +31,6 @@ func CoreTest(t *testing.T) { seven := fe.Exec(fe.Code(7), "seven", b.Reg).LatestXT() fe.Exec(3, seven, sq, b.Add) t.AssertEqual(fe.Pop(), 52) - //myvar := fe.Exec(fe.Code(fe.NewVar()), "myvar", b.Reg).Pop() myvar := fe.Exec("myvar", b.Var1).LatestXT() fe.Exec(5, myvar, b.Put) fe.Exec(seven, myvar, b.Get, b.Mult) diff --git a/tests/funky_test.go b/tests/funky_test.go deleted file mode 100644 index f0f707c..0000000 --- a/tests/funky_test.go +++ /dev/null @@ -1,42 +0,0 @@ -package scopes_test - -import ( - "fmt" - tbase "testing" - - "git.sr.ht/~cco/go-scopes/common/funky" - "git.sr.ht/~cco/go-scopes/common/testing" -) - -func TestFunky(tb *tbase.T) { - t := testing.SetUp(tb) - t.Run("maybe", MaybeTest) - t.Run("either", EitherTest) - t.Run("iterator", IteratorTest) -} - -func MaybeTest(t *testing.T) { - var i0 funky.Maybe[int] = funky.Nothing[int]() - t.AssertEqual(i0.IsNothing(), true) - var i1 funky.Maybe[int] = funky.Just(3) - t.AssertEqual(i1.IsNothing(), false) - t.AssertEqual(i1.Value(), 3) -} - -func EitherTest(t *testing.T) { - var e0 funky.Either[int] = funky.Value[int](7) - t.AssertEqual(e0.Ok(), true) - t.AssertEqual(e0.IsNothing(), false) - t.AssertEqual(e0.Value(), 7) - var e1 funky.Either[int] = funky.Error[int](fmt.Errorf("Some Error")) - t.AssertEqual(e1.Ok(), false) - t.AssertEqual(fmt.Sprint(e1.Error()), "Some Error") -} - -func IteratorTest(t *testing.T) { - sl := []int{3, 4, 5} - var it funky.Iterator[int] = funky.SliceIterator[int](sl) - s2 := funky.Slice(it) - t.AssertEqual(len(s2), 3) - t.AssertEqual(s2[1], 4) -}