forked from AndreyTsvetkov/Functional.Maybe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaybeLinq.cs
More file actions
131 lines (120 loc) · 4.57 KB
/
MaybeLinq.cs
File metadata and controls
131 lines (120 loc) · 4.57 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Diagnostics.Contracts;
namespace Functional.Maybe
{
/// <summary>
/// Providing necessary methods to enable linq syntax with Maybes themselves
/// </summary>
public static class MaybeLinq
{
/// <summary>
/// If <paramref name="a"/> has value, applies <paramref name="fn"/> to it and returns the result as Maybe, otherwise returns Nothing
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TResult"></typeparam>
/// <param name="a"></param>
/// <param name="fn"></param>
/// <returns></returns>
public static Maybe<TResult> Select<T, TResult>(this Maybe<T> a, Func<T, TResult> fn)
{
Contract.Requires(fn != null);
if (a.HasValue)
{
var result = fn(a.Value);
return result != null
? new Maybe<TResult>(result)
: Maybe<TResult>.Nothing;
}
else
return Maybe<TResult>.Nothing;
}
/// <summary>
/// If <paramref name="a"/> has value, applies <paramref name="fn"/> to it and returns the result, otherwise returns <paramref name="else"/>()
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TResult"></typeparam>
/// <param name="a"></param>
/// <param name="fn"></param>
/// <param name="else"></param>
/// <returns></returns>
public static TResult SelectOrElse<T, TResult>(this Maybe<T> a, Func<T, TResult> fn, Func<TResult> @else)
{
Contract.Requires(fn != null);
Contract.Requires(@else != null);
return a.HasValue ? fn(a.Value) : @else();
}
/// <summary>
/// If <paramref name="a"/> has value, and it fulfills the <paramref name="predicate"/>, returns <paramref name="a"/>, otherwise returns Nothing
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="a"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public static Maybe<T> Where<T>(this Maybe<T> a, Func<T, bool> predicate)
{
Contract.Requires(predicate != null);
if (!a.HasValue)
return a;
if (predicate(a.Value))
return a;
return Maybe<T>.Nothing;
}
/// <summary>
/// If <paramref name="a"/> has value, applies <paramref name="fn"/> to it and returns, otherwise returns Nothing
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TR"></typeparam>
/// <param name="a"></param>
/// <param name="fn"></param>
/// <returns></returns>
public static Maybe<TR> SelectMany<T, TR>(this Maybe<T> a, Func<T, Maybe<TR>> fn)
{
Contract.Requires(fn != null);
if (!a.HasValue)
return Maybe<TR>.Nothing;
return fn(a.Value);
}
/// <summary>
/// If <paramref name="a"/> has value, applies <paramref name="fn"/> to it, and if the result also has value, calls <paramref name="composer"/> on both values
/// (original and fn-call-resulted), and returns the <paramref name="composer"/>-call result, wrapped in Maybe. Otherwise returns nothing.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TTempResult"></typeparam>
/// <typeparam name="TResult"></typeparam>
/// <param name="a"></param>
/// <param name="fn"></param>
/// <param name="composer"></param>
/// <returns></returns>
public static Maybe<TResult> SelectMany<T, TTempResult, TResult>(this Maybe<T> a, Func<T, Maybe<TTempResult>> fn, Func<T, TTempResult, TResult> composer)
{
return a.SelectMany(x => fn(x).SelectMany(y => composer(x, y).ToMaybe()));
}
/// <summary>
/// If <paramref name="a"/> has value, applies <paramref name="fn"/> to it and returns, otherwise returns Nothing. Alias for SelectMany.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TR"></typeparam>
/// <param name="a"></param>
/// <param name="fn"></param>
/// <returns></returns>
public static Maybe<TR> SelectMaybe<T, TR>(this Maybe<T> a, Func<T, Maybe<TR>> fn)
{
return a.SelectMany(fn);
}
/// <summary>
/// If <paramref name="a"/> has value, applies <paramref name="fn"/> to it, and if the result also has value, calls <paramref name="composer"/> on both values
/// (original and fn-call-resulted), and returns the <paramref name="composer"/>-call result, wrapped in Maybe. Otherwise returns nothing.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TTempResult"></typeparam>
/// <typeparam name="TResult"></typeparam>
/// <param name="a"></param>
/// <param name="fn"></param>
/// <param name="composer"></param>
/// <returns></returns>
public static Maybe<TResult> SelectMaybe<T, TTempResult, TResult>(this Maybe<T> a, Func<T, Maybe<TTempResult>> fn, Func<T, TTempResult, TResult> composer)
{
return a.SelectMany(fn, composer);
}
}
}