1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-08-10 22:31:32 +02:00

fix: refactor either type (#102)

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
Carsten Leue
2024-02-07 11:03:20 +01:00
committed by GitHub
parent 7daf65effc
commit 8150ae2a68
18 changed files with 59 additions and 58 deletions

View File

@@ -23,17 +23,16 @@ type (
// Either defines a data structure that logically holds either an E or an A. The flag discriminates the cases
Either[E, A any] struct {
isLeft bool
left E
right A
value any
}
)
// String prints some debug info for the object
func (s Either[E, A]) String() string {
if s.isLeft {
return fmt.Sprintf("Left[%T, %T](%v)", s.left, s.right, s.left)
return fmt.Sprintf("Left[%T](%v)", s.value, s.value)
}
return fmt.Sprintf("Right[%T, %T](%v)", s.left, s.right, s.right)
return fmt.Sprintf("Right[%T](%v)", s.value, s.value)
}
// Format prints some debug info for the object
@@ -58,23 +57,29 @@ func IsRight[E, A any](val Either[E, A]) bool {
// Left creates a new instance of an [Either] representing the left value.
func Left[A, E any](value E) Either[E, A] {
return Either[E, A]{isLeft: true, left: value}
return Either[E, A]{true, value}
}
// Right creates a new instance of an [Either] representing the right value.
func Right[E, A any](value A) Either[E, A] {
return Either[E, A]{isLeft: false, right: value}
return Either[E, A]{false, value}
}
// MonadFold extracts the values from an [Either] by invoking the [onLeft] callback or the [onRight] callback depending on the case
func MonadFold[E, A, B any](ma Either[E, A], onLeft func(e E) B, onRight func(a A) B) B {
if ma.isLeft {
return onLeft(ma.left)
return onLeft(ma.value.(E))
}
return onRight(ma.right)
return onRight(ma.value.(A))
}
// Unwrap converts an [Either] into the idiomatic tuple
func Unwrap[E, A any](ma Either[E, A]) (A, E) {
return ma.right, ma.left
if ma.isLeft {
var a A
return a, ma.value.(E)
} else {
var e E
return ma.value.(A), e
}
}

View File

@@ -26,12 +26,6 @@ import (
"github.com/stretchr/testify/assert"
)
func TestDefault(t *testing.T) {
var e Either[error, string]
assert.Equal(t, Of[error](""), e)
}
func TestIsLeft(t *testing.T) {
err := errors.New("Some error")
withError := Left[string](err)

View File

@@ -48,11 +48,11 @@ func ExampleEither_creation() {
fmt.Println(rightFromPred)
// Output:
// Left[*errors.errorString, string](some error)
// Right[<nil>, string](value)
// Left[*errors.errorString, *string](value was nil)
// Left[*errors.errorString](some error)
// Right[string](value)
// Left[*errors.errorString](value was nil)
// true
// Left[*errors.errorString, int](3 is an odd number)
// Right[<nil>, int](4)
// Left[*errors.errorString](3 is an odd number)
// Right[int](4)
}

View File

@@ -53,8 +53,8 @@ func ExampleEither_extraction() {
fmt.Println(doubleFromRightBis)
// Output:
// Left[*errors.errorString, int](Division by Zero!)
// Right[<nil>, int](10)
// Left[*errors.errorString](Division by Zero!)
// Right[int](10)
// 0
// 10
// 0