From 1805fd6f19a53778542e13f8421214dece5a6920 Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Wed, 14 Aug 2024 18:02:51 -0700 Subject: [PATCH] fn: breaking - polish Either API --- fn/either.go | 10 +++++----- fn/either_test.go | 12 ++++++------ fn/result.go | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/fn/either.go b/fn/either.go index f85104385..19a7ec171 100644 --- a/fn/either.go +++ b/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 // 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]() } diff --git a/fn/either_test.go b/fn/either_test.go index 81af2bc6c..e8e29b6c1 100644 --- a/fn/either_test.go +++ b/fn/either_test.go @@ -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 } diff --git a/fn/result.go b/fn/result.go index 6328a7891..57c920039 100644 --- a/fn/result.go +++ b/fn/result.go @@ -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.