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
|
||||
}
|
||||
|
||||
// Either
|
||||
// Either (sometimes called `Result`)
|
||||
|
||||
type Either[V any] interface {
|
||||
Maybe[V]
|
||||
Ok() bool
|
||||
Right() V
|
||||
Left() error
|
||||
Value() V
|
||||
Error() error
|
||||
}
|
||||
|
||||
type either[V any] struct {
|
||||
ok bool
|
||||
right V
|
||||
left error
|
||||
value V
|
||||
error error
|
||||
}
|
||||
|
||||
func Right[V any](v V) Either[V] {
|
||||
func Value[V any](v V) Either[V] {
|
||||
return &either[V]{
|
||||
ok: true,
|
||||
right: v,
|
||||
value: v,
|
||||
}
|
||||
}
|
||||
|
||||
func Left[V any](e error) Either[V] {
|
||||
return &either[V]{left: e}
|
||||
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]) Right() V {
|
||||
return et.right
|
||||
func (et *either[V]) IsNothing() bool {
|
||||
return !et.Ok()
|
||||
}
|
||||
|
||||
func (et *either[V]) Left() error {
|
||||
return et.left
|
||||
func (et *either[V]) Value() V {
|
||||
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) {
|
||||
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.Right(), 7)
|
||||
var e1 funky.Either[int] = funky.Left[int](fmt.Errorf("Some Error"))
|
||||
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.Left()), "Some Error")
|
||||
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)
|
||||
s12 := funky.Slice(it)
|
||||
t.AssertEqual(len(s12), 3)
|
||||
t.AssertEqual(s12[1], 4)
|
||||
s2 := funky.Slice(it)
|
||||
t.AssertEqual(len(s2), 3)
|
||||
t.AssertEqual(s2[1], 4)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue