@@ -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.
1010func 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.
1918func 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.
2927type 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.
4340func (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.
4845func (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.
5653func (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.
6461func (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.
7370func (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.
8279func (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.
9188func (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.
10097func (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.
109106func (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.
118115func (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.
127124func (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.
136133func (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.
147144func (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] {
158155func (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
0 commit comments