-
Notifications
You must be signed in to change notification settings - Fork 449
/
Result.kt
236 lines (221 loc) · 6.47 KB
/
Result.kt
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
@file:OptIn(ExperimentalContracts::class)
package arrow.core
import kotlin.Result.Companion.failure
import kotlin.Result.Companion.success
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
@PublishedApi
internal inline val UnitResult: Result<Unit>
inline get() = success(Unit)
/**
* Compose a [transform] operation on the success value [A] into [B] whilst flattening [Result].
* @see mapCatching if you want run a function that catches and maps with `(A) -> B`
*/
public inline fun <A, B> Result<A>.flatMap(transform: (value: A) -> Result<B>): Result<B> {
contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) }
return map(transform).fold(::identity, ::failure)
}
/**
* Compose a recovering [transform] operation on the failure value [Throwable] whilst flattening [Result].
* @see recoverCatching if you want run a function that catches and maps recovers with `(Throwable) -> A`.
*/
public inline fun <A> Result<A>.handleErrorWith(transform: (throwable: Throwable) -> Result<A>): Result<A> {
contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) }
return when (val exception = exceptionOrNull()) {
null -> this
else -> transform(exception)
}
}
/**
* Compose both:
* - a [transform] operation on the success value [A] into [B] whilst flattening [Result].
* - a recovering [transform] operation on the failure value [Throwable] whilst flattening [Result].
*
* Combining the powers of [flatMap] and [handleErrorWith].
*/
public inline fun <A, B> Result<A>.redeemWith(
handleErrorWith: (throwable: Throwable) -> Result<B>,
transform: (value: A) -> Result<B>
): Result<B> {
contract {
callsInPlace(handleErrorWith, InvocationKind.AT_MOST_ONCE)
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
}
return fold(transform, handleErrorWith)
}
/**
* Combines n-arity independent [Result] values with a [transform] function.
*/
public inline fun <A, B, C> Result<A>.zip(b: Result<B>, transform: (A, B) -> C): Result<C> {
contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) }
return zip(
b,
UnitResult,
UnitResult,
UnitResult,
UnitResult,
UnitResult,
UnitResult,
UnitResult,
UnitResult
) { a, b, _, _, _, _, _, _, _, _ -> transform(a, b) }
}
public inline fun <A, B, C, D> Result<A>.zip(b: Result<B>, c: Result<C>, transform: (A, B, C) -> D): Result<D> {
contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) }
return zip(
b,
c,
UnitResult,
UnitResult,
UnitResult,
UnitResult,
UnitResult,
UnitResult,
UnitResult
) { a, b, c, _, _, _, _, _, _, _ -> transform(a, b, c) }
}
public inline fun <A, B, C, D, E> Result<A>.zip(
b: Result<B>,
c: Result<C>,
d: Result<D>,
transform: (A, B, C, D) -> E
): Result<E> {
contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) }
return zip(
b,
c,
d,
UnitResult,
UnitResult,
UnitResult,
UnitResult,
UnitResult,
UnitResult
) { a, b, c, d, _, _, _, _, _, _ -> transform(a, b, c, d) }
}
public inline fun <A, B, C, D, E, F> Result<A>.zip(
b: Result<B>,
c: Result<C>,
d: Result<D>,
e: Result<E>,
transform: (A, B, C, D, E) -> F
): Result<F> {
contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) }
return zip(b, c, d, e, UnitResult, UnitResult, UnitResult, UnitResult, UnitResult) { a, b, c, d, e, f, _, _, _, _ ->
transform(
a,
b,
c,
d,
e
)
}
}
public inline fun <A, B, C, D, E, F, G> Result<A>.zip(
b: Result<B>,
c: Result<C>,
d: Result<D>,
e: Result<E>,
f: Result<F>,
transform: (A, B, C, D, E, F) -> G
): Result<G> {
contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) }
return zip(b, c, d, e, f, UnitResult, UnitResult, UnitResult, UnitResult) { a, b, c, d, e, f, _, _, _, _ ->
transform(
a,
b,
c,
d,
e,
f
)
}
}
public inline fun <A, B, C, D, E, F, G, H> Result<A>.zip(
b: Result<B>,
c: Result<C>,
d: Result<D>,
e: Result<E>,
f: Result<F>,
g: Result<G>,
transform: (A, B, C, D, E, F, G) -> H
): Result<H> {
contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) }
return zip(b, c, d, e, f, g, UnitResult, UnitResult, UnitResult) { a, b, c, d, e, f, g, _, _, _ -> transform(a, b, c, d, e, f, g) }
}
public inline fun <A, B, C, D, E, F, G, H, I> Result<A>.zip(
b: Result<B>,
c: Result<C>,
d: Result<D>,
e: Result<E>,
f: Result<F>,
g: Result<G>,
h: Result<H>,
transform: (A, B, C, D, E, F, G, H) -> I
): Result<I> {
contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) }
return zip(b, c, d, e, f, g, h, UnitResult, UnitResult) { a, b, c, d, e, f, g, h, _, _ -> transform(a, b, c, d, e, f, g, h) }
}
public inline fun <A, B, C, D, E, F, G, H, I, J> Result<A>.zip(
b: Result<B>,
c: Result<C>,
d: Result<D>,
e: Result<E>,
f: Result<F>,
g: Result<G>,
h: Result<H>,
i: Result<I>,
transform: (A, B, C, D, E, F, G, H, I) -> J
): Result<J> {
contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) }
return zip(b, c, d, e, f, g, h, i, UnitResult) { a, b, c, d, e, f, g, h, i, _ -> transform(a, b, c, d, e, f, g, h, i) }
}
@Suppress("UNCHECKED_CAST")
public inline fun <A, B, C, D, E, F, G, H, I, J, K> Result<A>.zip(
b: Result<B>,
c: Result<C>,
d: Result<D>,
e: Result<E>,
f: Result<F>,
g: Result<G>,
h: Result<H>,
i: Result<I>,
j: Result<J>,
transform: (A, B, C, D, E, F, G, H, I, J) -> K,
): Result<K> {
contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) }
return if (isSuccess && b.isSuccess && c.isSuccess && d.isSuccess && e.isSuccess && f.isSuccess && g.isSuccess && h.isSuccess && i.isSuccess && j.isSuccess)
success(
transform(
getOrNull() as A,
b.getOrNull() as B,
c.getOrNull() as C,
d.getOrNull() as D,
e.getOrNull() as E,
f.getOrNull() as F,
g.getOrNull() as G,
h.getOrNull() as H,
i.getOrNull() as I,
j.getOrNull() as J
)
) else
composeErrors(
exceptionOrNull(),
b.exceptionOrNull(),
c.exceptionOrNull(),
d.exceptionOrNull(),
e.exceptionOrNull(),
f.exceptionOrNull(),
g.exceptionOrNull(),
h.exceptionOrNull(),
i.exceptionOrNull(),
j.exceptionOrNull(),
)!!.let(::failure)
}
@PublishedApi
internal fun composeErrors(vararg other: Throwable?): Throwable? =
other.reduceOrNull { a, b ->
Nullable.zip(a, b, Throwable::addSuppressed)
a ?: b
}