fn: breaking - polish Either API

This commit is contained in:
Keagan McClelland 2024-08-14 18:02:51 -07:00
parent c6734ea013
commit 1805fd6f19
No known key found for this signature in database
GPG Key ID: FA7E65C951F12439
3 changed files with 13 additions and 13 deletions

View File

@ -20,7 +20,7 @@ func NewRight[L any, R any](r R) Either[L, R] {
// ElimEither is the universal Either eliminator. It can be used to safely
// handle all possible values inside the Either by supplying two continuations,
// one for each side of the Either.
func ElimEither[L, R, O any](f func(L) O, g func(R) O, e Either[L, R]) O {
func ElimEither[L, R, O any](e Either[L, R], f func(L) O, g func(R) O) O {
if !e.isRight {
return f(e.left)
}
@ -52,9 +52,9 @@ func (e Either[L, R]) IsRight() bool {
return e.isRight
}
// LeftToOption converts a Left value to an Option, returning None if the inner
// LeftToSome converts a Left value to an Option, returning None if the inner
// Either value is a Right value.
func (e Either[L, R]) LeftToOption() Option[L] {
func (e Either[L, R]) LeftToSome() Option[L] {
if e.isRight {
return None[L]()
}
@ -62,9 +62,9 @@ func (e Either[L, R]) LeftToOption() Option[L] {
return Some(e.left)
}
// RightToOption converts a Right value to an Option, returning None if the
// RightToSome converts a Right value to an Option, returning None if the
// inner Either value is a Left value.
func (e Either[L, R]) RightToOption() Option[R] {
func (e Either[L, R]) RightToSome() Option[R] {
if !e.isRight {
return None[R]()
}

View File

@ -10,17 +10,17 @@ func TestPropConstructorEliminatorDuality(t *testing.T) {
Len := func(s string) int { return len(s) } // smh
if isRight {
v := ElimEither(
NewRight[int, string](s),
Iden[int],
Len,
NewRight[int, string](s),
)
return v == Len(s)
}
v := ElimEither(
NewLeft[int, string](i),
Iden[int],
Len,
NewLeft[int, string](i),
)
return v == i
}
@ -99,16 +99,16 @@ func TestPropToOptionIdentities(t *testing.T) {
if isRight {
e = NewRight[int, string](s)
r2O := e.RightToOption() == Some(s)
r2O := e.RightToSome() == Some(s)
o2R := e == OptionToRight(Some(s), i)
l2O := e.LeftToOption() == None[int]()
l2O := e.LeftToSome() == None[int]()
return r2O && o2R && l2O
} else {
e = NewLeft[int, string](i)
l2O := e.LeftToOption() == Some(i)
l2O := e.LeftToSome() == Some(i)
o2L := e == OptionToLeft(Some(i), s)
r2O := e.RightToOption() == None[string]()
r2O := e.RightToSome() == None[string]()
return l2O && o2L && r2O
}

View File

@ -99,12 +99,12 @@ func MapOk[A, B any](f func(A) B) func(Result[A]) Result[B] {
//
// Deprecated: Use OkToSome instead.
func (r Result[T]) Option() Option[T] {
return r.Either.LeftToOption()
return r.Either.LeftToSome()
}
// OkToSome mutes the error value of the result.
func (r Result[T]) OkToSome() Option[T] {
return r.Either.LeftToOption()
return r.Either.LeftToSome()
}
// WhenResult executes the given function if the Result is a success.