BadScript 2
Loading...
Searching...
No Matches
CSharpx.MaybeExtensions Class Reference

Provides convenience extension methods for CSharpx.Maybe. More...

Static Public Member Functions

static void Match< T > (this Maybe< T > maybe, Action< T > ifJust, Action ifNothing)
 Provides pattern matching using System.Action delegates.
 
static void Match< T1, T2 > (this Maybe< Tuple< T1, T2 > > maybe, Action< T1, T2 > ifJust, Action ifNothing)
 Provides pattern matching using System.Action delegates over maybe with tupled wrapped value.
 
static bool MatchJust< T1, T2 > (this Maybe< Tuple< T1, T2 > > maybe, out T1 value1, out T2 value2)
 Matches a value returning true and tupled value itself via two output parameters.
 
static Maybe< T > ToMaybe< T > (this T value)
 Equivalent to monadic CSharpx.Maybe.Return<T> operation. Builds a CSharpx.Just<T> value in case value is different from its default.
 
static Maybe< T2 > Bind< T1, T2 > (this Maybe< T1 > maybe, Func< T1, Maybe< T2 > > func)
 Invokes a function on this maybe value that itself yields a maybe.
 
static Maybe< T2 > Map< T1, T2 > (this Maybe< T1 > maybe, Func< T1, T2 > func)
 Transforms this maybe value by using a specified mapping function.
 
static Maybe< TResult > Select< TSource, TResult > (this Maybe< TSource > maybe, Func< TSource, TResult > selector)
 Map operation compatible with Linq.
 
static Maybe< TResult > SelectMany< TSource, TValue, TResult > (this Maybe< TSource > maybe, Func< TSource, Maybe< TValue > > valueSelector, Func< TSource, TValue, TResult > resultSelector)
 Bind operation compatible with Linq.
 
static void Do< T > (this Maybe< T > maybe, Action< T > action)
 If contains a value executes an System.Action<T> delegate over it.
 
static void Do< T1, T2 > (this Maybe< Tuple< T1, T2 > > maybe, Action< T1, T2 > action)
 If contans a value executes an System.Action<T1, T2> delegate over it.
 
static bool IsJust< T > (this Maybe< T > maybe)
 Returns true iffits argument is of the form CSharpx.Just<T>.
 
static bool IsNothing< T > (this Maybe< T > maybe)
 Returns true iffits argument is of the form CSharpx.Nothing<T>.
 
static T FromJust< T > (this Maybe< T > maybe)
 Extracts the element out of a CSharpx.Just<T> and returns a default value if its argument is CSharpx.Nothing<T>.
 
static T FromJustOrFail< T > (this Maybe< T > maybe, Exception exceptionToThrow=null)
 Extracts the element out of a CSharpx.Just<T> and throws an error if its argument is CSharpx.Nothing<T>.
 
static T GetValueOrDefault< T > (this Maybe< T > maybe, T noneValue)
 If contains a values returns it, otherwise returns noneValue .
 
static T2 MapValueOrDefault< T1, T2 > (this Maybe< T1 > maybe, Func< T1, T2 > func, T2 noneValue)
 If contains a values executes a mapping function over it, otherwise returns noneValue .
 
static T2 MapValueOrDefault< T1, T2 > (this Maybe< T1 > maybe, Func< T1, T2 > func, Func< T2 > noneValueFactory)
 If contains a values executes a mapping function over it, otherwise returns the value from noneValueFactory .
 
static IEnumerable< T > ToEnumerable< T > (this Maybe< T > maybe)
 Returns an empty list when given CSharpx.Nothing<T> or a singleton list when given a CSharpx.Just<T>.
 

Detailed Description

Provides convenience extension methods for CSharpx.Maybe.

Definition at line 201 of file Maybe.cs.

Member Function Documentation

◆ Bind< T1, T2 >()

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

Invokes a function on this maybe value that itself yields a maybe.

Definition at line 275 of file Maybe.cs.

276 {
277 return Maybe.Bind(maybe, func);
278 }

◆ Do< T >()

static void CSharpx.MaybeExtensions.Do< T > ( this Maybe< T >  maybe,
Action< T >  action 
)
static

If contains a value executes an System.Action<T> delegate over it.

Definition at line 320 of file Maybe.cs.

321 {
322 T value;
323
324 if (maybe.MatchJust(out value))
325 {
326 action(value);
327 }
328 }

◆ Do< T1, T2 >()

static void CSharpx.MaybeExtensions.Do< T1, T2 > ( this Maybe< Tuple< T1, T2 > >  maybe,
Action< T1, T2 >  action 
)
static

If contans a value executes an System.Action<T1, T2> delegate over it.

Definition at line 333 of file Maybe.cs.

334 {
335 T1 value1;
336 T2 value2;
337
338 if (maybe.MatchJust(out value1, out value2))
339 {
340 action(value1, value2);
341 }
342 }

◆ FromJust< T >()

static T CSharpx.MaybeExtensions.FromJust< T > ( this Maybe< T >  maybe)
static

Extracts the element out of a CSharpx.Just<T> and returns a default value if its argument is CSharpx.Nothing<T>.

Definition at line 366 of file Maybe.cs.

367 {
368 T value;
369
370 if (maybe.MatchJust(out value))
371 {
372 return value;
373 }
374
375 return default;
376 }

◆ FromJustOrFail< T >()

static T CSharpx.MaybeExtensions.FromJustOrFail< T > ( this Maybe< T >  maybe,
Exception  exceptionToThrow = null 
)
static

Extracts the element out of a CSharpx.Just<T> and throws an error if its argument is CSharpx.Nothing<T>.

Definition at line 382 of file Maybe.cs.

383 {
384 T value;
385
386 if (maybe.MatchJust(out value))
387 {
388 return value;
389 }
390
391 throw exceptionToThrow ?? new ArgumentException("Value empty.");
392 }

◆ GetValueOrDefault< T >()

static T CSharpx.MaybeExtensions.GetValueOrDefault< T > ( this Maybe< T >  maybe,
noneValue 
)
static

If contains a values returns it, otherwise returns noneValue .

Definition at line 397 of file Maybe.cs.

398 {
399 T value;
400
401 return maybe.MatchJust(out value) ? value : noneValue;
402 }

◆ IsJust< T >()

static bool CSharpx.MaybeExtensions.IsJust< T > ( this Maybe< T >  maybe)
static

Returns true iffits argument is of the form CSharpx.Just<T>.

Definition at line 349 of file Maybe.cs.

350 {
351 return maybe.Tag == MaybeType.Just;
352 }
MaybeType
Discriminator for CSharpx.Maybe.
Definition Maybe.cs:19

◆ IsNothing< T >()

static bool CSharpx.MaybeExtensions.IsNothing< T > ( this Maybe< T >  maybe)
static

Returns true iffits argument is of the form CSharpx.Nothing<T>.

Definition at line 357 of file Maybe.cs.

358 {
359 return maybe.Tag == MaybeType.Nothing;
360 }

◆ Map< T1, T2 >()

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

Transforms this maybe value by using a specified mapping function.

Definition at line 283 of file Maybe.cs.

284 {
285 return Maybe.Map(maybe, func);
286 }

◆ MapValueOrDefault< T1, T2 >() [1/2]

static T2 CSharpx.MaybeExtensions.MapValueOrDefault< T1, T2 > ( this Maybe< T1 >  maybe,
Func< T1, T2 >  func,
Func< T2 >  noneValueFactory 
)
static

If contains a values executes a mapping function over it, otherwise returns the value from noneValueFactory .

Definition at line 418 of file Maybe.cs.

419 {
420 T1 value1;
421
422 return maybe.MatchJust(out value1) ? func(value1) : noneValueFactory();
423 }

◆ MapValueOrDefault< T1, T2 >() [2/2]

static T2 CSharpx.MaybeExtensions.MapValueOrDefault< T1, T2 > ( this Maybe< T1 >  maybe,
Func< T1, T2 >  func,
T2  noneValue 
)
static

If contains a values executes a mapping function over it, otherwise returns noneValue .

Definition at line 407 of file Maybe.cs.

408 {
409 T1 value1;
410
411 return maybe.MatchJust(out value1) ? func(value1) : noneValue;
412 }

◆ Match< T >()

static void CSharpx.MaybeExtensions.Match< T > ( this Maybe< T >  maybe,
Action< T >  ifJust,
Action  ifNothing 
)
static

Provides pattern matching using System.Action delegates.

Definition at line 208 of file Maybe.cs.

209 {
210 T value;
211
212 if (maybe.MatchJust(out value))
213 {
214 ifJust(value);
215
216 return;
217 }
218
219 ifNothing();
220 }

◆ Match< T1, T2 >()

static void CSharpx.MaybeExtensions.Match< T1, T2 > ( this Maybe< Tuple< T1, T2 > >  maybe,
Action< T1, T2 >  ifJust,
Action  ifNothing 
)
static

Provides pattern matching using System.Action delegates over maybe with tupled wrapped value.

Definition at line 225 of file Maybe.cs.

226 {
227 T1 value1;
228 T2 value2;
229
230 if (maybe.MatchJust(out value1, out value2))
231 {
232 ifJust(value1, value2);
233
234 return;
235 }
236
237 ifNothing();
238 }

◆ MatchJust< T1, T2 >()

static bool CSharpx.MaybeExtensions.MatchJust< T1, T2 > ( this Maybe< Tuple< T1, T2 > >  maybe,
out T1  value1,
out T2  value2 
)
static

Matches a value returning true and tupled value itself via two output parameters.

Definition at line 243 of file Maybe.cs.

244 {
245 Tuple<T1, T2> value;
246
247 if (maybe.MatchJust(out value))
248 {
249 value1 = value.Item1;
250 value2 = value.Item2;
251
252 return true;
253 }
254
255 value1 = default;
256 value2 = default;
257
258 return false;
259 }

◆ Select< TSource, TResult >()

static Maybe< TResult > CSharpx.MaybeExtensions.Select< TSource, TResult > ( this Maybe< TSource >  maybe,
Func< TSource, TResult >  selector 
)
static

Map operation compatible with Linq.

Definition at line 293 of file Maybe.cs.

295 {
296 return Maybe.Map(maybe, selector);
297 }

◆ SelectMany< TSource, TValue, TResult >()

static Maybe< TResult > CSharpx.MaybeExtensions.SelectMany< TSource, TValue, TResult > ( this Maybe< TSource >  maybe,
Func< TSource, Maybe< TValue > >  valueSelector,
Func< TSource, TValue, TResult >  resultSelector 
)
static

Bind operation compatible with Linq.

Definition at line 302 of file Maybe.cs.

305 {
306 return maybe
307 .Bind(sourceValue =>
308 valueSelector(sourceValue)
309 .Map(resultValue => resultSelector(sourceValue, resultValue))
310 );
311 }

◆ ToEnumerable< T >()

static IEnumerable< T > CSharpx.MaybeExtensions.ToEnumerable< T > ( this Maybe< T >  maybe)
static

Returns an empty list when given CSharpx.Nothing<T> or a singleton list when given a CSharpx.Just<T>.

Definition at line 429 of file Maybe.cs.

430 {
431 T value;
432
433 if (maybe.MatchJust(out value))
434 {
435 return Enumerable.Empty<T>()
436 .Concat(new[] { value });
437 }
438
439 return Enumerable.Empty<T>();
440 }

◆ ToMaybe< T >()

static Maybe< T > CSharpx.MaybeExtensions.ToMaybe< T > ( this T  value)
static

Equivalent to monadic CSharpx.Maybe.Return<T> operation. Builds a CSharpx.Just<T> value in case value is different from its default.

Definition at line 267 of file Maybe.cs.

268 {
269 return Maybe.Return(value);
270 }

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