forked from AndreyTsvetkov/Functional.Maybe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maybe.cs
executable file
·110 lines (98 loc) · 2.65 KB
/
Maybe.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
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Generic;
namespace Functional.Maybe
{
/// <summary>
/// The option type; explicitly represents nothing-or-thing nature of a value.
/// Supports some of the LINQ operators, such as SelectMany, Where and can be used
/// with linq syntax:
/// </summary>
/// <example>
/// // gets sum of the first and last elements, if they are present, orelse «-5»;
///
/// Maybe<int> maybeA = list.FirstMaybe();
/// Maybe<int> maybeB = list.LastMaybe();
/// int result = (
/// from a in maybeA
/// from b in maybeB
/// select a + b
/// ).OrElse(-5);
///
/// // or shorter:
/// var result = (from a in list.FirstMaybe() from b in list.LastMaybe() select a + b).OrElse(-5);
/// </example>
/// <typeparam name="T"></typeparam>
[Serializable]
public struct Maybe<T> : IEquatable<Maybe<T>>
{
/// <summary>
/// Nothing value.
/// </summary>
public static readonly Maybe<T> Nothing = new Maybe<T>();
/// <summary>
/// The value, stored in the monad. Can be accessed only if is really present, otherwise throws
/// </summary>
/// <exception cref="InvalidOperationException"> is thrown if not value is present</exception>
public T Value
{
get
{
if (!HasValue) throw new InvalidOperationException("value is not present");
return _value;
}
}
/// <summary>
/// The flag of value presence
/// </summary>
public bool HasValue { get { return _hasValue; } }
/// <inheritdoc />
public override string ToString()
{
if (!HasValue)
{
return "<Nothing>";
}
return Value.ToString();
}
/// <summary>
/// Automatical flattening of the monad-in-monad
/// </summary>
/// <param name="doubleMaybe"></param>
/// <returns></returns>
public static implicit operator Maybe<T>(Maybe<Maybe<T>> doubleMaybe)
{
return doubleMaybe.HasValue ? doubleMaybe.Value : Nothing;
}
internal Maybe(T value)
{
_value = value;
_hasValue = true;
}
public bool Equals(Maybe<T> other)
{
return EqualityComparer<T>.Default.Equals(_value, other._value) && _hasValue.Equals(other._hasValue);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
return obj is Maybe<T> && Equals((Maybe<T>) obj);
}
public override int GetHashCode()
{
unchecked
{
return (EqualityComparer<T>.Default.GetHashCode(_value)*397) ^ _hasValue.GetHashCode();
}
}
public static bool operator ==(Maybe<T> left, Maybe<T> right)
{
return left.Equals(right);
}
public static bool operator !=(Maybe<T> left, Maybe<T> right)
{
return !left.Equals(right);
}
private readonly T _value;
private readonly bool _hasValue;
}
}