BadScript 2
Loading...
Searching...
No Matches
CSharpx.Either< TLeft, TRight > Class Template Referenceabstract
Inheritance diagram for CSharpx.Either< TLeft, TRight >:
CSharpx.Left< TLeft, TRight > CSharpx.Right< TLeft, TRight >

Public Member Functions

bool MatchLeft (out TLeft value)
 
bool MatchRight (out TRight value)
 

Static Public Member Functions

static Either< TLeft, TResult > Map< TLeft, TRight, TResult > (Either< TLeft, TRight > either, Func< TRight, TResult > func)
 Transforms a Either's right value by using a specified mapping function.
 
static Either< TLeft1, TRight1 > Bimap< TLeft, TRight, TLeft1, TRight1 > (Either< TLeft, TRight > either, Func< TLeft, TLeft1 > mapLeft, Func< TRight, TRight1 > mapRight)
 Maps both parts of a Either type. Applies the first function if Either is Left. Otherwise applies the second function.
 
static TRight GetOrFail< TLeft, TRight > (Either< TLeft, TRight > either)
 Returns a Either Right or fail with an exception.
 
static TLeft GetLeftOrDefault< TLeft, TRight > (Either< TLeft, TRight > either, TLeft @default)
 Returns a Either Left or a defualt value.
 
static TRight GetRightOrDefault< TLeft, TRight > (Either< TLeft, TRight > either, TRight @default)
 Returns a Either Right or a defualt value.
 
static Either< Exception, TRight > Try< TRight > (Func< TRight > func)
 Wraps a function, encapsulates any exception thrown within to a Either.
 
static Either< Exception, TRight > Cast< TRight > (object obj)
 Attempts to cast an object. Stores the cast value in 1Of2 if successful, otherwise stores the exception in 2Of2.
 
static Either< TLeft, TRight > FromMaybe< TLeft, TRight > (Maybe< TRight > maybe, TLeft left)
 
static Either< TLeft, TRight > Left< TLeft, TRight > (TLeft value)
 
static Either< TLeft, TRight > Right< TLeft, TRight > (TRight value)
 
static Either< string, TRight > Return< TRight > (TRight value)
 Inject a value into the Either type, returning Right case.
 
static Either< string, TRight > Fail< TRight > (string message)
 Fail with a message. Not part of mathematical definition of a monad.
 
static Either< TLeft, TResult > Bind< TLeft, TRight, TResult > (Either< TLeft, TRight > either, Func< TRight, Either< TLeft, TResult > > func)
 Monadic bind.
 
static Either< TLeft, TResult > Select< TLeft, TRight, TResult > (this Either< TLeft, TRight > either, Func< TRight, TResult > selector)
 Map operation compatible with Linq.
 
static Either< TLeft, TResult > SelectMany< TLeft, TRight, TResult > (this Either< TLeft, TRight > result, Func< TRight, Either< TLeft, TResult > > func)
 

Protected Member Functions

 Either (EitherType tag)
 

Properties

EitherType Tag [get]
 

Static Private Member Functions

static TLeft GetLeft< TLeft, TRight > (this Either< TLeft, TRight > either)
 

Detailed Description

Definition at line 29 of file Either.cs.

Constructor & Destructor Documentation

◆ Either()

CSharpx.Either< TLeft, TRight >.Either ( EitherType  tag)
protected

Definition at line 31 of file Either.cs.

32 {
33 Tag = tag;
34 }
EitherType Tag
Definition Either.cs:36

Member Function Documentation

◆ Bimap< TLeft, TRight, TLeft1, TRight1 >()

static Either< TLeft1, TRight1 > CSharpx.Either< TLeft, TRight >.Bimap< TLeft, TRight, TLeft1, TRight1 > ( Either< TLeft, TRight >  either,
Func< TLeft, TLeft1 >  mapLeft,
Func< TRight, TRight1 >  mapRight 
)
static

Maps both parts of a Either type. Applies the first function if Either is Left. Otherwise applies the second function.

Definition at line 119 of file Either.cs.

123 {
124 TRight right;
125
126 if (either.MatchRight(out right))
127 {
128 return Right<TLeft1, TRight1>(mapRight(right));
129 }
130
131 return Left<TLeft1, TRight1>(mapLeft(either.GetLeft()));
132 }

◆ Bind< TLeft, TRight, TResult >()

static Either< TLeft, TResult > CSharpx.Either< TLeft, TRight >.Bind< TLeft, TRight, TResult > ( Either< TLeft, TRight >  either,
Func< TRight, Either< TLeft, TResult > >  func 
)
static

Monadic bind.

Definition at line 247 of file Either.cs.

250 {
251 TRight right;
252
253 if (either.MatchRight(out right))
254 {
255 return func(right);
256 }
257
258 return Left<TLeft, TResult>(either.GetLeft());
259 }

◆ Cast< TRight >()

static Either< Exception, TRight > CSharpx.Either< TLeft, TRight >.Cast< TRight > ( object  obj)
static

Attempts to cast an object. Stores the cast value in 1Of2 if successful, otherwise stores the exception in 2Of2.

Definition at line 190 of file Either.cs.

191 {
192 return Try(() => (TRight)obj);
193 }

◆ Fail< TRight >()

static Either< string, TRight > CSharpx.Either< TLeft, TRight >.Fail< TRight > ( string  message)
static

Fail with a message. Not part of mathematical definition of a monad.

Definition at line 239 of file Either.cs.

240 {
241 throw new Exception(message);
242 }

◆ FromMaybe< TLeft, TRight >()

static Either< TLeft, TRight > CSharpx.Either< TLeft, TRight >.FromMaybe< TLeft, TRight > ( Maybe< TRight >  maybe,
TLeft  left 
)
static

Definition at line 196 of file Either.cs.

197 {
198 if (maybe.Tag == MaybeType.Just)
199 {
200 return Right<TLeft, TRight>(((Just<TRight>)maybe).Value);
201 }
202
203 return Left<TLeft, TRight>(left);
204 }
static Either< TLeft, TRight > Left< TLeft, TRight >(TLeft value)
Definition Either.cs:214
static Either< TLeft, TRight > Right< TLeft, TRight >(TRight value)
Definition Either.cs:219
MaybeType
Discriminator for CSharpx.Maybe.
Definition Maybe.cs:19

◆ GetLeft< TLeft, TRight >()

static TLeft CSharpx.Either< TLeft, TRight >.GetLeft< TLeft, TRight > ( this Either< TLeft, TRight >  either)
staticprivate

Definition at line 207 of file Either.cs.

208 {
209 return ((Left<TLeft, TRight>)either).Value;
210 }

◆ GetLeftOrDefault< TLeft, TRight >()

static TLeft CSharpx.Either< TLeft, TRight >.GetLeftOrDefault< TLeft, TRight > ( Either< TLeft, TRight >  either,
TLeft @  default 
)
static

Returns a Either Left or a defualt value.

Definition at line 154 of file Either.cs.

155 {
156 TLeft value;
157
158 return either.MatchLeft(out value) ? value : @default;
159 }

◆ GetOrFail< TLeft, TRight >()

static TRight CSharpx.Either< TLeft, TRight >.GetOrFail< TLeft, TRight > ( Either< TLeft, TRight >  either)
static

Returns a Either Right or fail with an exception.

Definition at line 139 of file Either.cs.

140 {
141 TRight value;
142
143 if (either.MatchRight(out value))
144 {
145 return value;
146 }
147
148 throw new ArgumentException(nameof(either), string.Format("The either value was Left {0}", either));
149 }

◆ GetRightOrDefault< TLeft, TRight >()

static TRight CSharpx.Either< TLeft, TRight >.GetRightOrDefault< TLeft, TRight > ( Either< TLeft, TRight >  either,
TRight @  default 
)
static

Returns a Either Right or a defualt value.

Definition at line 164 of file Either.cs.

165 {
166 TRight value;
167
168 return either.MatchRight(out value) ? value : @default;
169 }

◆ Left< TLeft, TRight >()

static Either< TLeft, TRight > CSharpx.Either< TLeft, TRight >.Left< TLeft, TRight > ( TLeft  value)
static

Definition at line 214 of file Either.cs.

215 {
216 return new Left<TLeft, TRight>(value);
217 }

◆ Map< TLeft, TRight, TResult >()

static Either< TLeft, TResult > CSharpx.Either< TLeft, TRight >.Map< TLeft, TRight, TResult > ( Either< TLeft, TRight >  either,
Func< TRight, TResult >  func 
)
static

Transforms a Either's right value by using a specified mapping function.

Definition at line 97 of file Either.cs.

100 {
101 TRight right;
102
103 if (either.MatchRight(out right))
104 {
105 return Right<TLeft, TResult>(func(right));
106 }
107
108 return Left<TLeft, TResult>(either.GetLeft());
109 }

◆ MatchLeft()

bool CSharpx.Either< TLeft, TRight >.MatchLeft ( out TLeft  value)

Definition at line 40 of file Either.cs.

41 {
42 value = Tag == EitherType.Left ? ((Left<TLeft, TRight>)this).Value : default;
43
44 return Tag == EitherType.Left;
45 }
EitherType
Definition Either.cs:14

◆ MatchRight()

bool CSharpx.Either< TLeft, TRight >.MatchRight ( out TRight  value)

Definition at line 47 of file Either.cs.

48 {
49 value = Tag == EitherType.Right ? ((Right<TLeft, TRight>)this).Value : default;
50
51 return Tag == EitherType.Right;
52 }

◆ Return< TRight >()

static Either< string, TRight > CSharpx.Either< TLeft, TRight >.Return< TRight > ( TRight  value)
static

Inject a value into the Either type, returning Right case.

Definition at line 231 of file Either.cs.

232 {
233 return Right<string, TRight>(value);
234 }

◆ Right< TLeft, TRight >()

static Either< TLeft, TRight > CSharpx.Either< TLeft, TRight >.Right< TLeft, TRight > ( TRight  value)
static

Definition at line 219 of file Either.cs.

220 {
221 return new Right<TLeft, TRight>(value);
222 }

◆ Select< TLeft, TRight, TResult >()

static Either< TLeft, TResult > CSharpx.Either< TLeft, TRight >.Select< TLeft, TRight, TResult > ( this Either< TLeft, TRight >  either,
Func< TRight, TResult >  selector 
)
static

Map operation compatible with Linq.

Definition at line 268 of file Either.cs.

270 {
271 return Map(either, selector);
272 }

◆ SelectMany< TLeft, TRight, TResult >()

static Either< TLeft, TResult > CSharpx.Either< TLeft, TRight >.SelectMany< TLeft, TRight, TResult > ( this Either< TLeft, TRight >  result,
Func< TRight, Either< TLeft, TResult > >  func 
)
static

Definition at line 274 of file Either.cs.

276 {
277 return Bind(result, func);
278 }

◆ Try< TRight >()

static Either< Exception, TRight > CSharpx.Either< TLeft, TRight >.Try< TRight > ( Func< TRight >  func)
static

Wraps a function, encapsulates any exception thrown within to a Either.

Definition at line 174 of file Either.cs.

175 {
176 try
177 {
178 return new Right<Exception, TRight>(func());
179 }
180 catch (Exception ex)
181 {
182 return new Left<Exception, TRight>(ex);
183 }
184 }

Property Documentation

◆ Tag

EitherType CSharpx.Either< TLeft, TRight >.Tag
get

Definition at line 36 of file Either.cs.

36{ get; }

The documentation for this class was generated from the following file: