remove obsolete 'funky' package
This commit is contained in:
parent
0027b21a44
commit
591191deb0
5 changed files with 1 additions and 147 deletions
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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) {
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
Loading…
Add table
Reference in a new issue