BadScript 2
Loading...
Searching...
No Matches
CSharpx.Maybe< T > Class Template Referenceabstract

The Maybe type models an optional value. A value of type Maybe a either contains a value of type a (represented as Just a), or it is empty (represented as Nothing). More...

Inheritance diagram for CSharpx.Maybe< T >:
CSharpx.Just< T > CSharpx.Nothing< T >

Public Member Functions

bool MatchJust (out T value)
 Matches a value returning true and value itself via output parameter.
 
bool MatchNothing ()
 Matches an empty value returning true.
 

Static Public Member Functions

static Maybe< T > Nothing< T > ()
 Builds the empty case of CSharpx.Maybe.
 
static Just< T > Just< T > (T value)
 Builds the case when CSharpx.Maybe contains a value.
 
static Maybe< T > Return< T > (T value)
 Inject a value into the monadic CSharpx.Maybe<T> type.
 
static Maybe< T2 > Bind< T1, T2 > (Maybe< T1 > maybe, Func< T1, Maybe< T2 > > func)
 Sequentially compose two actions, passing any value produced by the first as an argument to the second.
 
static Maybe< T2 > Map< T1, T2 > (Maybe< T1 > maybe, Func< T1, T2 > func)
 Transforms an maybe value by using a specified mapping function.
 
static Maybe< Tuple< T1, T2 > > Merge< T1, T2 > (Maybe< T1 > first, Maybe< T2 > second)
 If both maybes contain a value, it merges them into a maybe with a tupled value.
 
static Maybe< TRight > FromEither< TLeft, TRight > (Either< TLeft, TRight > either)
 Maps Either Right value to Maybe Just, otherwise Maybe Nothing.
 

Protected Member Functions

 Maybe (MaybeType tag)
 

Properties

MaybeType Tag [get]
 Type discriminator.
 

Detailed Description

The Maybe type models an optional value. A value of type Maybe a either contains a value of type a (represented as Just a), or it is empty (represented as Nothing).

Provides static methods for manipulating CSharpx.Maybe.

Definition at line 32 of file Maybe.cs.

Constructor & Destructor Documentation

◆ Maybe()

CSharpx.Maybe< T >.Maybe ( MaybeType  tag)
protected

Definition at line 34 of file Maybe.cs.

35 {
36 Tag = tag;
37 }
MaybeType Tag
Type discriminator.
Definition Maybe.cs:42

Member Function Documentation

◆ Bind< T1, T2 >()

static Maybe< T2 > CSharpx.Maybe< T >.Bind< T1, T2 > ( Maybe< T1 >  maybe,
Func< T1, Maybe< T2 > >  func 
)
static

Sequentially compose two actions, passing any value produced by the first as an argument to the second.

Definition at line 142 of file Maybe.cs.

143 {
144 T1 value1;
145
146 return maybe.MatchJust(out value1) ? func(value1) : Nothing<T2>();
147 }

◆ FromEither< TLeft, TRight >()

static Maybe< TRight > CSharpx.Maybe< T >.FromEither< TLeft, TRight > ( Either< TLeft, TRight >  either)
static

Maps Either Right value to Maybe Just, otherwise Maybe Nothing.

Definition at line 185 of file Maybe.cs.

186 {
187 if (either.Tag == EitherType.Right) {
188 return Maybe.Just(((Right<TLeft, TRight>)either).Value);
189 }
190 return Maybe.Nothing<TRight>();
191 }
Maybe(MaybeType tag)
Definition Maybe.cs:34
EitherType
Definition Either.cs:14

◆ Just< T >()

static Just< T > CSharpx.Maybe< T >.Just< T > ( value)
static

Builds the case when CSharpx.Maybe contains a value.

Definition at line 122 of file Maybe.cs.

123 {
124 return new Just<T>(value);
125 }
static Just< T > Just< T >(T value)
Builds the case when CSharpx.Maybe contains a value.
Definition Maybe.cs:122

◆ Map< T1, T2 >()

static Maybe< T2 > CSharpx.Maybe< T >.Map< T1, T2 > ( Maybe< T1 >  maybe,
Func< T1, T2 >  func 
)
static

Transforms an maybe value by using a specified mapping function.

Definition at line 156 of file Maybe.cs.

157 {
158 T1 value1;
159
160 return maybe.MatchJust(out value1) ? Just(func(value1)) : Nothing<T2>();
161 }

◆ MatchJust()

bool CSharpx.Maybe< T >.MatchJust ( out T  value)

Matches a value returning true and value itself via output parameter.

Definition at line 49 of file Maybe.cs.

50 {
51 value = Tag == MaybeType.Just ? ((Just<T>)this).Value : default;
52
53 return Tag == MaybeType.Just;
54 }
MaybeType
Discriminator for CSharpx.Maybe.
Definition Maybe.cs:19

◆ MatchNothing()

bool CSharpx.Maybe< T >.MatchNothing ( )

Matches an empty value returning true.

Definition at line 59 of file Maybe.cs.

60 {
61 return Tag == MaybeType.Nothing;
62 }

◆ Merge< T1, T2 >()

static Maybe< Tuple< T1, T2 > > CSharpx.Maybe< T >.Merge< T1, T2 > ( Maybe< T1 >  first,
Maybe< T2 >  second 
)
static

If both maybes contain a value, it merges them into a maybe with a tupled value.

Definition at line 168 of file Maybe.cs.

169 {
170 T1 value1;
171 T2 value2;
172
173 if (first.MatchJust(out value1) && second.MatchJust(out value2))
174 {
175 return Just(Tuple.Create(value1, value2));
176 }
177
178 return Nothing<Tuple<T1, T2>>();
179 }

◆ Nothing< T >()

static Maybe< T > CSharpx.Maybe< T >.Nothing< T > ( )
static

Builds the empty case of CSharpx.Maybe.

Definition at line 114 of file Maybe.cs.

115 {
116 return new Nothing<T>();
117 }
static Maybe< T > Nothing< T >()
Builds the empty case of CSharpx.Maybe.
Definition Maybe.cs:114

◆ Return< T >()

static Maybe< T > CSharpx.Maybe< T >.Return< T > ( value)
static

Inject a value into the monadic CSharpx.Maybe<T> type.

Definition at line 134 of file Maybe.cs.

135 {
136 return Equals(value, default(T)) ? Nothing<T>() : Just(value);
137 }

Property Documentation

◆ Tag

MaybeType CSharpx.Maybe< T >.Tag
get

Type discriminator.

Definition at line 42 of file Maybe.cs.

42{ get; }

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