rename Either methods, functions, fields
This commit is contained in:
parent
c54562b998
commit
3f16eb44ca
2 changed files with 26 additions and 20 deletions
|
@ -30,39 +30,44 @@ func (m *maybe[V]) Value() V {
|
||||||
return m.value
|
return m.value
|
||||||
}
|
}
|
||||||
|
|
||||||
// Either
|
// Either (sometimes called `Result`)
|
||||||
|
|
||||||
type Either[V any] interface {
|
type Either[V any] interface {
|
||||||
|
Maybe[V]
|
||||||
Ok() bool
|
Ok() bool
|
||||||
Right() V
|
Value() V
|
||||||
Left() error
|
Error() error
|
||||||
}
|
}
|
||||||
|
|
||||||
type either[V any] struct {
|
type either[V any] struct {
|
||||||
ok bool
|
ok bool
|
||||||
right V
|
value V
|
||||||
left error
|
error error
|
||||||
}
|
}
|
||||||
|
|
||||||
func Right[V any](v V) Either[V] {
|
func Value[V any](v V) Either[V] {
|
||||||
return &either[V]{
|
return &either[V]{
|
||||||
ok: true,
|
ok: true,
|
||||||
right: v,
|
value: v,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Left[V any](e error) Either[V] {
|
func Error[V any](e error) Either[V] {
|
||||||
return &either[V]{left: e}
|
return &either[V]{error: e}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (et *either[V]) Ok() bool {
|
func (et *either[V]) Ok() bool {
|
||||||
return et.ok
|
return et.ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func (et *either[V]) Right() V {
|
func (et *either[V]) IsNothing() bool {
|
||||||
return et.right
|
return !et.Ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (et *either[V]) Left() error {
|
func (et *either[V]) Value() V {
|
||||||
return et.left
|
return et.value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (et *either[V]) Error() error {
|
||||||
|
return et.error
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,18 +24,19 @@ func MaybeTest(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func EitherTest(t *testing.T) {
|
func EitherTest(t *testing.T) {
|
||||||
var e0 funky.Either[int] = funky.Right[int](7)
|
var e0 funky.Either[int] = funky.Value[int](7)
|
||||||
t.AssertEqual(e0.Ok(), true)
|
t.AssertEqual(e0.Ok(), true)
|
||||||
t.AssertEqual(e0.Right(), 7)
|
t.AssertEqual(e0.IsNothing(), false)
|
||||||
var e1 funky.Either[int] = funky.Left[int](fmt.Errorf("Some Error"))
|
t.AssertEqual(e0.Value(), 7)
|
||||||
|
var e1 funky.Either[int] = funky.Error[int](fmt.Errorf("Some Error"))
|
||||||
t.AssertEqual(e1.Ok(), false)
|
t.AssertEqual(e1.Ok(), false)
|
||||||
t.AssertEqual(fmt.Sprint(e1.Left()), "Some Error")
|
t.AssertEqual(fmt.Sprint(e1.Error()), "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)
|
||||||
s12 := funky.Slice(it)
|
s2 := funky.Slice(it)
|
||||||
t.AssertEqual(len(s12), 3)
|
t.AssertEqual(len(s2), 3)
|
||||||
t.AssertEqual(s12[1], 4)
|
t.AssertEqual(s2[1], 4)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue