Skip to content

Commit a9c34fb

Browse files
authored
Merge pull request #5 from herulume/chore/clean-either-internal-state
chore: clean Either internal state
2 parents b282dea + a7d99ea commit a9c34fb

File tree

3 files changed

+38
-41
lines changed

3 files changed

+38
-41
lines changed

either.go

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,23 @@ var eitherMissingRightValue = fmt.Errorf("no such Right value")
99
// Left builds the left side of the Either struct, as opposed to the Right side.
1010
func Left[L any, R any](value L) Either[L, R] {
1111
return Either[L, R]{
12-
isLeft: true,
13-
isRight: false,
14-
left: value,
12+
isLeft: true,
13+
left: value,
1514
}
1615
}
1716

1817
// Right builds the right side of the Either struct, as opposed to the Left side.
1918
func Right[L any, R any](value R) Either[L, R] {
2019
return Either[L, R]{
21-
isLeft: false,
22-
isRight: true,
23-
right: value,
20+
isLeft: false,
21+
right: value,
2422
}
2523
}
2624

2725
// Either respresents a value of 2 possible types.
2826
// An instance of Either is an instance of either A or B.
2927
type Either[L any, R any] struct {
30-
isLeft bool
31-
isRight bool
28+
isLeft bool
3229

3330
left L
3431
right R
@@ -41,28 +38,28 @@ func (e Either[L, R]) IsLeft() bool {
4138

4239
// IsRight returns true if Either is an instance of Right.
4340
func (e Either[L, R]) IsRight() bool {
44-
return e.isRight
41+
return !e.isLeft
4542
}
4643

4744
// Left returns left value of a Either struct.
4845
func (e Either[L, R]) Left() (L, bool) {
49-
if e.isLeft {
46+
if e.IsLeft() {
5047
return e.left, true
5148
}
5249
return empty[L](), false
5350
}
5451

5552
// Right returns right value of a Either struct.
5653
func (e Either[L, R]) Right() (R, bool) {
57-
if e.isRight {
54+
if e.IsRight() {
5855
return e.right, true
5956
}
6057
return empty[R](), false
6158
}
6259

6360
// MustLeft returns left value of a Either struct or panics.
6461
func (e Either[L, R]) MustLeft() L {
65-
if !e.isLeft {
62+
if !e.IsLeft() {
6663
panic(eitherMissingLeftValue)
6764
}
6865

@@ -71,7 +68,7 @@ func (e Either[L, R]) MustLeft() L {
7168

7269
// MustRight returns right value of a Either struct or panics.
7370
func (e Either[L, R]) MustRight() R {
74-
if !e.isRight {
71+
if !e.IsRight() {
7572
panic(eitherMissingRightValue)
7673
}
7774

@@ -80,7 +77,7 @@ func (e Either[L, R]) MustRight() R {
8077

8178
// LeftOrElse returns left value of a Either struct or fallback.
8279
func (e Either[L, R]) LeftOrElse(fallback L) L {
83-
if e.isLeft {
80+
if e.IsLeft() {
8481
return e.left
8582
}
8683

@@ -89,7 +86,7 @@ func (e Either[L, R]) LeftOrElse(fallback L) L {
8986

9087
// RightOrElse returns right value of a Either struct or fallback.
9188
func (e Either[L, R]) RightOrElse(fallback R) R {
92-
if e.isRight {
89+
if e.IsRight() {
9390
return e.right
9491
}
9592

@@ -98,7 +95,7 @@ func (e Either[L, R]) RightOrElse(fallback R) R {
9895

9996
// LeftOrEmpty returns left value of a Either struct or empty value.
10097
func (e Either[L, R]) LeftOrEmpty() L {
101-
if e.isLeft {
98+
if e.IsLeft() {
10299
return e.left
103100
}
104101

@@ -107,7 +104,7 @@ func (e Either[L, R]) LeftOrEmpty() L {
107104

108105
// RightOrEmpty returns right value of a Either struct or empty value.
109106
func (e Either[L, R]) RightOrEmpty() R {
110-
if e.isRight {
107+
if e.IsRight() {
111108
return e.right
112109
}
113110

@@ -116,7 +113,7 @@ func (e Either[L, R]) RightOrEmpty() R {
116113

117114
// Swap returns the left value in Right and vice versa.
118115
func (e Either[L, R]) Swap() Either[R, L] {
119-
if e.isLeft {
116+
if e.IsLeft() {
120117
return Right[R, L](e.left)
121118
}
122119

@@ -125,18 +122,18 @@ func (e Either[L, R]) Swap() Either[R, L] {
125122

126123
// ForEach executes the given side-effecting function, depending of value is Left or Right.
127124
func (e Either[L, R]) ForEach(leftCb func(L), rightCb func(R)) {
128-
if e.isLeft {
125+
if e.IsLeft() {
129126
leftCb(e.left)
130-
} else if e.isRight {
127+
} else if e.IsRight() {
131128
rightCb(e.right)
132129
}
133130
}
134131

135132
// Match executes the given function, depending of value is Left or Right, and returns result.
136133
func (e Either[L, R]) Match(onLeft func(L) Either[L, R], onRight func(R) Either[L, R]) Either[L, R] {
137-
if e.isLeft {
134+
if e.IsLeft() {
138135
return onLeft(e.left)
139-
} else if e.isRight {
136+
} else if e.IsRight() {
140137
return onRight(e.right)
141138
}
142139

@@ -145,9 +142,9 @@ func (e Either[L, R]) Match(onLeft func(L) Either[L, R], onRight func(R) Either[
145142

146143
// MapLeft executes the given function, if Either is of type Left, and returns result.
147144
func (e Either[L, R]) MapLeft(mapper func(L) Either[L, R]) Either[L, R] {
148-
if e.isLeft {
145+
if e.IsLeft() {
149146
return mapper(e.left)
150-
} else if e.isRight {
147+
} else if e.IsRight() {
151148
return Right[L, R](e.right)
152149
}
153150

@@ -158,7 +155,7 @@ func (e Either[L, R]) MapLeft(mapper func(L) Either[L, R]) Either[L, R] {
158155
func (e Either[L, R]) MapRight(mapper func(R) Either[L, R]) Either[L, R] {
159156
if e.isLeft {
160157
return Left[L, R](e.left)
161-
} else if e.isRight {
158+
} else if e.IsRight() {
162159
return mapper(e.right)
163160
}
164161

either_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ func TestEitherLeft(t *testing.T) {
1010
is := assert.New(t)
1111

1212
left := Left[int, bool](42)
13-
is.Equal(Either[int, bool]{left: 42, right: false, isLeft: true, isRight: false}, left)
13+
is.Equal(Either[int, bool]{left: 42, right: false, isLeft: true}, left)
1414
}
1515

1616
func TestEitherRight(t *testing.T) {
1717
is := assert.New(t)
1818

1919
right := Right[int, bool](true)
20-
is.Equal(Either[int, bool]{left: 0, right: true, isLeft: false, isRight: true}, right)
20+
is.Equal(Either[int, bool]{left: 0, right: true, isLeft: false}, right)
2121
}
2222

2323
func TestEitherIsLeftOrRight(t *testing.T) {
@@ -103,8 +103,8 @@ func TestEitherSwap(t *testing.T) {
103103
left := Left[int, string](42)
104104
right := Right[int, string]("foobar")
105105

106-
is.Equal(Either[string, int]{left: "", right: 42, isLeft: false, isRight: true}, left.Swap())
107-
is.Equal(Either[string, int]{left: "foobar", right: 0, isLeft: true, isRight: false}, right.Swap())
106+
is.Equal(Either[string, int]{left: "", right: 42, isLeft: false}, left.Swap())
107+
is.Equal(Either[string, int]{left: "foobar", right: 0, isLeft: true}, right.Swap())
108108
}
109109

110110
func TestEitherForEach(t *testing.T) {
@@ -154,8 +154,8 @@ func TestEitherMatch(t *testing.T) {
154154
},
155155
)
156156

157-
is.Equal(Either[int, string]{left: 21, right: "", isLeft: true, isRight: false}, e1)
158-
is.Equal(Either[int, string]{left: 0, right: "plop", isLeft: false, isRight: true}, e2)
157+
is.Equal(Either[int, string]{left: 21, right: "", isLeft: true}, e1)
158+
is.Equal(Either[int, string]{left: 0, right: "plop", isLeft: false}, e2)
159159
}
160160

161161
func TestEitherMapLeft(t *testing.T) {
@@ -175,8 +175,8 @@ func TestEitherMapLeft(t *testing.T) {
175175
},
176176
)
177177

178-
is.Equal(Either[int, string]{left: 21, right: "", isLeft: true, isRight: false}, e1)
179-
is.Equal(Either[int, string]{left: 0, right: "foobar", isLeft: false, isRight: true}, e2)
178+
is.Equal(Either[int, string]{left: 21, right: "", isLeft: true}, e1)
179+
is.Equal(Either[int, string]{left: 0, right: "foobar", isLeft: false}, e2)
180180
}
181181

182182
func TestEitherMapRight(t *testing.T) {
@@ -196,6 +196,6 @@ func TestEitherMapRight(t *testing.T) {
196196
},
197197
)
198198

199-
is.Equal(Either[int, string]{left: 42, right: "", isLeft: true, isRight: false}, e1)
200-
is.Equal(Either[int, string]{left: 0, right: "plop", isLeft: false, isRight: true}, e2)
199+
is.Equal(Either[int, string]{left: 42, right: "", isLeft: true}, e1)
200+
is.Equal(Either[int, string]{left: 0, right: "plop", isLeft: false}, e2)
201201
}

io_either_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestIOEither(t *testing.T) {
1616

1717
is.False(result.isLeft)
1818
is.Nil(result.Left())
19-
is.True(result.isRight)
19+
is.True(result.IsRight())
2020
is.Equal(42, result.MustRight())
2121
}
2222

@@ -31,7 +31,7 @@ func TestIOEither1(t *testing.T) {
3131

3232
is.False(result.isLeft)
3333
is.Nil(result.Left())
34-
is.True(result.isRight)
34+
is.True(result.IsRight())
3535
is.Equal(42, result.MustRight())
3636
}
3737

@@ -47,7 +47,7 @@ func TestIOEither2(t *testing.T) {
4747

4848
is.False(result.isLeft)
4949
is.Nil(result.Left())
50-
is.True(result.isRight)
50+
is.True(result.IsRight())
5151
is.Equal(42, result.MustRight())
5252
}
5353

@@ -64,7 +64,7 @@ func TestIOEither3(t *testing.T) {
6464

6565
is.False(result.isLeft)
6666
is.Nil(result.Left())
67-
is.True(result.isRight)
67+
is.True(result.IsRight())
6868
is.Equal(42, result.MustRight())
6969
}
7070

@@ -82,7 +82,7 @@ func TestIOEither4(t *testing.T) {
8282

8383
is.False(result.isLeft)
8484
is.Nil(result.Left())
85-
is.True(result.isRight)
85+
is.True(result.IsRight())
8686
is.Equal(42, result.MustRight())
8787
}
8888

@@ -101,6 +101,6 @@ func TestIOEither5(t *testing.T) {
101101

102102
is.False(result.isLeft)
103103
is.Nil(result.Left())
104-
is.True(result.isRight)
104+
is.True(result.IsRight())
105105
is.Equal(42, result.MustRight())
106106
}

0 commit comments

Comments
 (0)