mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-27 22:21:18 +02:00
fn: breaking - polish Either API
This commit is contained in:
10
fn/either.go
10
fn/either.go
@@ -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
|
// ElimEither is the universal Either eliminator. It can be used to safely
|
||||||
// handle all possible values inside the Either by supplying two continuations,
|
// handle all possible values inside the Either by supplying two continuations,
|
||||||
// one for each side of the Either.
|
// 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 {
|
if !e.isRight {
|
||||||
return f(e.left)
|
return f(e.left)
|
||||||
}
|
}
|
||||||
@@ -52,9 +52,9 @@ func (e Either[L, R]) IsRight() bool {
|
|||||||
return e.isRight
|
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.
|
// 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 {
|
if e.isRight {
|
||||||
return None[L]()
|
return None[L]()
|
||||||
}
|
}
|
||||||
@@ -62,9 +62,9 @@ func (e Either[L, R]) LeftToOption() Option[L] {
|
|||||||
return Some(e.left)
|
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.
|
// 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 {
|
if !e.isRight {
|
||||||
return None[R]()
|
return None[R]()
|
||||||
}
|
}
|
||||||
|
@@ -10,17 +10,17 @@ func TestPropConstructorEliminatorDuality(t *testing.T) {
|
|||||||
Len := func(s string) int { return len(s) } // smh
|
Len := func(s string) int { return len(s) } // smh
|
||||||
if isRight {
|
if isRight {
|
||||||
v := ElimEither(
|
v := ElimEither(
|
||||||
|
NewRight[int, string](s),
|
||||||
Iden[int],
|
Iden[int],
|
||||||
Len,
|
Len,
|
||||||
NewRight[int, string](s),
|
|
||||||
)
|
)
|
||||||
return v == Len(s)
|
return v == Len(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
v := ElimEither(
|
v := ElimEither(
|
||||||
|
NewLeft[int, string](i),
|
||||||
Iden[int],
|
Iden[int],
|
||||||
Len,
|
Len,
|
||||||
NewLeft[int, string](i),
|
|
||||||
)
|
)
|
||||||
return v == i
|
return v == i
|
||||||
}
|
}
|
||||||
@@ -99,16 +99,16 @@ func TestPropToOptionIdentities(t *testing.T) {
|
|||||||
if isRight {
|
if isRight {
|
||||||
e = NewRight[int, string](s)
|
e = NewRight[int, string](s)
|
||||||
|
|
||||||
r2O := e.RightToOption() == Some(s)
|
r2O := e.RightToSome() == Some(s)
|
||||||
o2R := e == OptionToRight(Some(s), i)
|
o2R := e == OptionToRight(Some(s), i)
|
||||||
l2O := e.LeftToOption() == None[int]()
|
l2O := e.LeftToSome() == None[int]()
|
||||||
|
|
||||||
return r2O && o2R && l2O
|
return r2O && o2R && l2O
|
||||||
} else {
|
} else {
|
||||||
e = NewLeft[int, string](i)
|
e = NewLeft[int, string](i)
|
||||||
l2O := e.LeftToOption() == Some(i)
|
l2O := e.LeftToSome() == Some(i)
|
||||||
o2L := e == OptionToLeft(Some(i), s)
|
o2L := e == OptionToLeft(Some(i), s)
|
||||||
r2O := e.RightToOption() == None[string]()
|
r2O := e.RightToSome() == None[string]()
|
||||||
|
|
||||||
return l2O && o2L && r2O
|
return l2O && o2L && r2O
|
||||||
}
|
}
|
||||||
|
@@ -99,12 +99,12 @@ func MapOk[A, B any](f func(A) B) func(Result[A]) Result[B] {
|
|||||||
//
|
//
|
||||||
// Deprecated: Use OkToSome instead.
|
// Deprecated: Use OkToSome instead.
|
||||||
func (r Result[T]) Option() Option[T] {
|
func (r Result[T]) Option() Option[T] {
|
||||||
return r.Either.LeftToOption()
|
return r.Either.LeftToSome()
|
||||||
}
|
}
|
||||||
|
|
||||||
// OkToSome mutes the error value of the result.
|
// OkToSome mutes the error value of the result.
|
||||||
func (r Result[T]) OkToSome() Option[T] {
|
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.
|
// WhenResult executes the given function if the Result is a success.
|
||||||
|
Reference in New Issue
Block a user