forked from AndreyTsvetkov/Functional.Maybe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaybeConvertions.cs
100 lines (93 loc) · 2.96 KB
/
MaybeConvertions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
namespace Functional.Maybe
{
/// <summary>
/// Fluent exts for converting the values of Maybe to/from lists, nullables; casting and upshifting
/// </summary>
public static class MaybeConvertions
{
/// <summary>
/// If <paramref name="a"/>.Value exists and can be successfully casted to <typeparamref name="TB"/>, returns the casted one, wrapped as Maybe<TB>, otherwise Nothing
/// </summary>
/// <typeparam name="TA"></typeparam>
/// <typeparam name="TB"></typeparam>
/// <param name="a"></param>
/// <returns></returns>
public static Maybe<TB> Cast<TA, TB>(this Maybe<TA> a) where TB : class
{
return from m in a
let t = m as TB
where t != null
select t;
}
/// <summary>
/// If <paramref name="a"/> can be successfully casted to <typeparamref name="TR"/>, returns the casted one, wrapped as Maybe<TR>, otherwise Nothing
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TR"></typeparam>
/// <param name="a"></param>
/// <returns></returns>
public static Maybe<TR> MaybeCast<T, TR>(this T a) where TR : T
{
return MaybeFunctionalWrappers.Catcher<T, TR, InvalidCastException>(o => (TR)o)(a);
}
/// <summary>
/// If <paramref name="a"/>.Value is present, returns a list of that single value, otherwise an empty list
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="a"></param>
/// <returns></returns>
public static IEnumerable<T> ReturnList<T>(this Maybe<T> a)
{
if (a.IsSomething())
yield return a.Value;
}
/// <summary>
/// If <paramref name="xs"/> contains any items, returns first one wrapped as Maybe, elsewhere returns Nothing
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="xs"></param>
/// <returns></returns>
public static Maybe<T> ToMaybeFromList<T>(this IEnumerable<T> xs)
{
return xs.FirstMaybe();
}
/// <summary>
/// Converts Maybe to corresponding Nullable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="a"></param>
/// <returns></returns>
public static T? ToNullable<T>(this Maybe<T> a) where T : struct
{
return a.IsSomething() ? a.Value : new T?();
}
/// <summary>
/// Converts Nullable to corresponding Maybe
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="a"></param>
/// <returns></returns>
public static Maybe<T> ToMaybe<T>(this T? a) where T : struct
{
if (!a.HasValue)
return Maybe<T>.Nothing;
return a.Value.ToMaybe();
}
/// <summary>
/// Returns <paramref name="a"/> wrapped as Maybe
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="a"></param>
/// <returns></returns>
public static Maybe<T> ToMaybe<T>(this T a)
{
// ReSharper disable CompareNonConstrainedGenericWithNull
if (a == null)
return Maybe<T>.Nothing;
return new Maybe<T>(a);
// ReSharper restore CompareNonConstrainedGenericWithNull
}
}
}