add new funky type
This commit is contained in:
parent
de6535a273
commit
c54562b998
2 changed files with 48 additions and 0 deletions
|
@ -29,3 +29,40 @@ func (m *maybe[V]) IsNothing() bool {
|
||||||
func (m *maybe[V]) Value() V {
|
func (m *maybe[V]) Value() V {
|
||||||
return m.value
|
return m.value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Either
|
||||||
|
|
||||||
|
type Either[V any] interface {
|
||||||
|
Ok() bool
|
||||||
|
Right() V
|
||||||
|
Left() error
|
||||||
|
}
|
||||||
|
|
||||||
|
type either[V any] struct {
|
||||||
|
ok bool
|
||||||
|
right V
|
||||||
|
left error
|
||||||
|
}
|
||||||
|
|
||||||
|
func Right[V any](v V) Either[V] {
|
||||||
|
return &either[V]{
|
||||||
|
ok: true,
|
||||||
|
right: v,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Left[V any](e error) Either[V] {
|
||||||
|
return &either[V]{left: e}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (et *either[V]) Ok() bool {
|
||||||
|
return et.ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func (et *either[V]) Right() V {
|
||||||
|
return et.right
|
||||||
|
}
|
||||||
|
|
||||||
|
func (et *either[V]) Left() error {
|
||||||
|
return et.left
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package scopes_test
|
package scopes_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
tbase "testing"
|
tbase "testing"
|
||||||
|
|
||||||
"git.sr.ht/~cco/go-scopes/lib/funky"
|
"git.sr.ht/~cco/go-scopes/lib/funky"
|
||||||
|
@ -10,6 +11,7 @@ import (
|
||||||
func TestFunky(tb *tbase.T) {
|
func TestFunky(tb *tbase.T) {
|
||||||
t := testing.SetUp(tb)
|
t := testing.SetUp(tb)
|
||||||
t.Run("maybe", MaybeTest)
|
t.Run("maybe", MaybeTest)
|
||||||
|
t.Run("either", EitherTest)
|
||||||
t.Run("iterator", IteratorTest)
|
t.Run("iterator", IteratorTest)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,6 +23,15 @@ func MaybeTest(t *testing.T) {
|
||||||
t.AssertEqual(i1.Value(), 3)
|
t.AssertEqual(i1.Value(), 3)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func EitherTest(t *testing.T) {
|
||||||
|
var e0 funky.Either[int] = funky.Right[int](7)
|
||||||
|
t.AssertEqual(e0.Ok(), true)
|
||||||
|
t.AssertEqual(e0.Right(), 7)
|
||||||
|
var e1 funky.Either[int] = funky.Left[int](fmt.Errorf("Some Error"))
|
||||||
|
t.AssertEqual(e1.Ok(), false)
|
||||||
|
t.AssertEqual(fmt.Sprint(e1.Left()), "Some Error")
|
||||||
|
}
|
||||||
|
|
||||||
func IteratorTest(t *testing.T) {
|
func IteratorTest(t *testing.T) {
|
||||||
sl := []int{3, 4, 5}
|
sl := []int{3, 4, 5}
|
||||||
var it funky.Iterator[int] = funky.SliceIterator[int](sl)
|
var it funky.Iterator[int] = funky.SliceIterator[int](sl)
|
||||||
|
|
Loading…
Add table
Reference in a new issue