-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonad.ts
147 lines (128 loc) · 4.15 KB
/
monad.ts
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
export interface Monad<T> {
/**
* Transform the value of this monad into
* a new value from the given function.
* Does nothing if Monad has no value
* @param f the mapping function
*/
map<U>(f: (t: T) => U): Monad<U>
/**
* If this Monad contains a value, apply
* the given Monad-bearing function flattening
* the result into a single Monad containing the
* value of the Monad resulting from the function
* @param f the function resulting in a Monad
*/
flatMap<U>(f: (t: T) => Monad<U>): Monad<U>
/**
* If a value is present in this Monad, apply
* the given predicate function to the value in
* this Monad. If the predicate function evaluates
* to true then the resulting Monad will retain the
* value of the original Monad. Else an empty
* Monad will be returned
* @param f the predicate function
*/
filter(f: (t: T) => boolean): Monad<T>
/**
* If this Monad has a value, return it;
* else return undefined
*/
getOrUndefined(): T | undefined
/**
* If this Monad has a value, return it;
* else return the non-null value provided
* as an argument to this method
* @param t the default value
*/
getOrDefault(t: T): T
/**
* If this Monad doesn't have a value, invoke
* the given supplier and return it's value
* @param t the supplier function
*/
orElseGet(t: () => T): T
/**
* If this Monad doesn't have a value,
* invoke the given supplier and throw the
* error it provides
* @param t the supplier function
*/
orElseThrow(t: () => Error): void
/**
* Return true if this Monad has a value
*/
hasValue(): boolean
/**
* Return true if this Monad is Empty
*/
isEmpty(): boolean
/**
* Perform a side-effect if this Monad doesn't
* contain a value
* @param f the function to apply
*/
doIfEmpty(f: () => void): Monad<T>
/**
* Perform a side-effect if this Monad contains
* a value
* @param f the consumer function apply
*/
doIfPresent(f: (t: T) => void): Monad<T>
/**
* Perform a side-effect if this Monad contains
* an Error
* @param f the error consumer
*/
doOnError(f: (t: T) => void): Monad<T>
/**
* Perform a side-effect if this Monad contains
* an Error which matches the given predicate function
* @param p the predicate function
* @param f the error consumer
*/
doOnErrorMatching(p: (t: T) => boolean, f: (t: T) => void): Monad<T>
/**
* Map to an alternative value if this Monad contains an
* error, otherwise retain this Monad
* @param f the mapping function providing the alternative
* value
*/
onErrorMap<U>(f: (t: T) => U): Monad<U>
/**
* Map to an alternative value if this Monad contains an
* error and it matches the given predicate function,
* otherwise retain this Monad
* @param p the predicate function
* @param f the mapping function providing the alternative
* value
*/
onErrorMapMatching<U>(p: (t: T) => boolean, f: (t: T) => U): Monad<U>
/**
* Map to an alternative Monad if this Monad contains an
* error, otherwise retain this Monad
* @param f the mapping function providing the alternative
* Monad
*/
onErrorFlatMap<U>(f: (t: T) => Monad<U>): Monad<U>
/**
* Map to an alternative Monad if this Monad contains an
* error and it matches the given predicate function,
* otherwise retain this Monad
* @param p the predicate function
* @param f the mapping function providing the alternative
* Monad
*/
onErrorFlatMapMatching<U>(p: (t: T) => boolean, f: (t: T) => Monad<U>): Monad<U>
/**
* Switch to an alternative value if the this Monad is empty
* otherwise retain this value
* @param u the value to provide to a new Monad
*/
switchIfEmpty<U>(u: U): Monad<U>
/**
* Switch to an alternative Monad if the this Monad is empty
* @param f the supplier of the alternative Monad
*/
or<U>(f: () => Monad<U>): Monad<U>
}