forked from AndreyTsvetkov/Functional.Maybe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaybeBoolean.cs
More file actions
62 lines (59 loc) · 1.82 KB
/
MaybeBoolean.cs
File metadata and controls
62 lines (59 loc) · 1.82 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
using System;
namespace Functional.Maybe
{
/// <summary>
/// Ternary logic with Maybe<bool> and combining T and bool to a Maybe value
/// </summary>
public static class MaybeBoolean
{
/// <summary>
/// If <paramref name="condition"/> returns <paramref name="f"/>() as Maybe, otherwise Nothing
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="condition"></param>
/// <param name="f"></param>
/// <returns></returns>
public static Maybe<T> Then<T>(this bool condition, Func<T> f)
{
// ReSharper disable CompareNonConstrainedGenericWithNull
return condition ? f().ToMaybe() : Maybe<T>.Nothing;
// ReSharper restore CompareNonConstrainedGenericWithNull
}
/// <summary>
/// If <paramref name="condition"/> returns <paramref name="t"/> as Maybe, otherwise Nothing
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="condition"></param>
/// <param name="t"></param>
/// <returns></returns>
public static Maybe<T> Then<T>(this bool condition, T t)
{
// ReSharper disable CompareNonConstrainedGenericWithNull
return condition ? t.ToMaybe() : Maybe<T>.Nothing;
// ReSharper restore CompareNonConstrainedGenericWithNull
}
/// <summary>
/// Calls <paramref name="fn"/> if <paramref name="m"/> is true.ToMaybe()
/// </summary>
/// <param name="m"></param>
/// <param name="fn"></param>
public static void DoWhenTrue(this Maybe<bool> m, Action fn)
{
if (m.HasValue && m.Value)
fn();
}
/// <summary>
/// Calls <paramref name="fn"/> if <paramref name="m"/> is true.ToMaybe()
/// </summary>
/// <param name="m"></param>
/// <param name="fn"></param>
/// <param name="else"></param>
public static void DoWhenTrue(this Maybe<bool> m, Action fn, Action @else)
{
if (m.HasValue && m.Value)
fn();
else
@else();
}
}
}