-
Notifications
You must be signed in to change notification settings - Fork 451
/
Copy pathEagerEffectScope.kt
283 lines (268 loc) · 8.86 KB
/
EagerEffectScope.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package arrow.core.continuations
import arrow.core.Either
import arrow.core.EmptyValue
import arrow.core.None
import arrow.core.Option
import arrow.core.Some
import arrow.core.Validated
import arrow.core.identity
import arrow.core.raise.fold
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
import kotlin.coroutines.RestrictsSuspension
import kotlin.experimental.ExperimentalTypeInference
import kotlin.jvm.JvmInline
/** Context of the [EagerEffect] DSL. */
@Deprecated(
"Use the arrow.core.raise.Raise type instead, which is more general and can be used to and can be used to raise typed errors or _logical failures_\n" +
"The Raise<R> type is source compatible, a simple find & replace of arrow.core.continuations.* to arrow.core.raise.* will do the trick.",
ReplaceWith("Raise<R>", "arrow.core.raise.Raise")
)
@RestrictsSuspension
public interface EagerEffectScope<in R> {
/** Short-circuit the [EagerEffect] computation with value [R].
*
* ```kotlin
* import arrow.core.continuations.eagerEffect
* import io.kotest.assertions.fail
* import io.kotest.matchers.shouldBe
*
* fun main() {
* eagerEffect<String, Int> {
* shift("SHIFT ME")
* }.fold({ it shouldBe "SHIFT ME" }, { fail("Computation never finishes") })
* }
* ```
* <!--- KNIT example-eager-effect-scope-01.kt -->
*/
public suspend fun <B> shift(r: R): B
/**
* Runs the [EagerEffect] to finish, returning [B] or [shift] in case of [R].
*
* ```kotlin
* import arrow.core.Either
* import arrow.core.continuations.EagerEffect
* import arrow.core.continuations.eagerEffect
* import arrow.core.identity
* import io.kotest.matchers.shouldBe
*
* fun <E, A> Either<E, A>.toEagerEffect(): EagerEffect<E, A> = eagerEffect {
* fold({ e -> shift(e) }, ::identity)
* }
*
* fun main() {
* val either = Either.Left("failed")
* eagerEffect<String, Int> {
* val x: Int = either.toEagerEffect().bind()
* x
* }.toEither() shouldBe either
* }
* ```
* <!--- KNIT example-eager-effect-scope-02.kt -->
*/
public suspend fun <B> EagerEffect<R, B>.bind(): B {
var left: Any? = EmptyValue
var right: Any? = EmptyValue
fold({ r -> left = r }, { a -> right = a })
return if (left === EmptyValue) EmptyValue.unbox(right) else shift(EmptyValue.unbox(left))
}
public suspend fun <B> arrow.core.raise.EagerEffect<R, B>.bind(): B =
fold({ shift(it) }, ::identity)
/**
* Folds [Either] into [EagerEffect], by returning [B] or a shift with [R].
*
* ```kotlin
* import arrow.core.Either
* import arrow.core.continuations.eagerEffect
* import io.kotest.matchers.shouldBe
*
* fun main() {
* val either = Either.Right(9)
* eagerEffect<String, Int> {
* val x: Int = either.bind()
* x
* }.toEither() shouldBe either
* }
* ```
* <!--- KNIT example-eager-effect-scope-03.kt -->
*/
public suspend fun <B> Either<R, B>.bind(): B =
when (this) {
is Either.Left -> shift(value)
is Either.Right -> value
}
/**
* Folds [Validated] into [EagerEffect], by returning [B] or a shift with [R].
*
* ```kotlin
* import arrow.core.Validated
* import arrow.core.continuations.eagerEffect
* import io.kotest.matchers.shouldBe
*
* fun main() {
* val validated = Validated.Valid(40)
* eagerEffect<String, Int> {
* val x: Int = validated.bind()
* x
* }.toValidated() shouldBe validated
* }
* ```
* <!--- KNIT example-eager-effect-scope-04.kt -->
*/
public suspend fun <B> Validated<R, B>.bind(): B =
when (this) {
is Validated.Valid -> value
is Validated.Invalid -> shift(value)
}
/**
* Folds [Result] into [EagerEffect], by returning [B] or a transforming [Throwable] into [R] and
* shifting the result.
*
* ```kotlin
* import arrow.core.continuations.eagerEffect
* import arrow.core.identity
* import io.kotest.matchers.shouldBe
*
* private val default = "failed"
* fun main() {
* val result = Result.success(1)
* eagerEffect<String, Int> {
* val x: Int = result.bind { _: Throwable -> default }
* x
* }.fold({ default }, ::identity) shouldBe result.getOrElse { default }
* }
* ```
* <!--- KNIT example-eager-effect-scope-05.kt -->
*/
public suspend fun <B> Result<B>.bind(transform: (Throwable) -> R): B =
fold(::identity) { throwable -> shift(transform(throwable)) }
/**
* Folds [Option] into [EagerEffect], by returning [B] or a transforming [None] into [R] and shifting the
* result.
*
* ```kotlin
* import arrow.core.None
* import arrow.core.Option
* import arrow.core.continuations.eagerEffect
* import arrow.core.getOrElse
* import arrow.core.identity
* import io.kotest.matchers.shouldBe
*
* private val default = "failed"
* fun main() {
* val option: Option<Int> = None
* eagerEffect<String, Int> {
* val x: Int = option.bind { default }
* x
* }.fold({ default }, ::identity) shouldBe option.getOrElse { default }
* }
* ```
* <!--- KNIT example-eager-effect-scope-06.kt -->
*/
public suspend fun <B> Option<B>.bind(shift: () -> R): B =
when (this) {
None -> shift(shift())
is Some -> value
}
/**
* ensure that condition is `true`, if it's `false` it will `shift` with the provided value [R].
* Monadic version of [kotlin.require].
*
* ```kotlin
* import arrow.core.Either
* import arrow.core.continuations.eagerEffect
* import io.kotest.matchers.shouldBe
*
* fun main() {
* val condition = true
* val failure = "failed"
* val int = 4
* eagerEffect<String, Int> {
* ensure(condition) { failure }
* int
* }.toEither() shouldBe if(condition) Either.Right(int) else Either.Left(failure)
* }
* ```
* <!--- KNIT example-eager-effect-scope-07.kt -->
*/
public suspend fun ensure(condition: Boolean, shift: () -> R): Unit =
if (condition) Unit else shift(shift())
/**
* Encloses an action for which you want to catch any `shift`.
* [attempt] is used in combination with [catch].
*
* ```
* attempt { ... } catch { ... }
* ```
*
* The [f] may `shift` into a different `EagerEffectScope`, giving
* the chance for a later [catch] to change the shifted value.
* This is useful to simulate re-throwing of exceptions.
*/
@OptIn(ExperimentalTypeInference::class)
public suspend fun <E, A> attempt(
@BuilderInference
f: suspend EagerEffectScope<E>.() -> A,
): suspend EagerEffectScope<E>.() -> A = f
/**
* When the [EagerEffect] has shifted with [R] it will [recover]
* the shifted value to [A], and when it ran the computation to
* completion it will return the value [A].
* [catch] is used in combination with [attempt].
*
* ```kotlin
* import arrow.core.Either
* import arrow.core.None
* import arrow.core.Option
* import arrow.core.Validated
* import arrow.core.continuations.eagerEffect
* import io.kotest.assertions.fail
* import io.kotest.matchers.shouldBe
*
* fun main() {
* eagerEffect<String, Int> {
* val x = Either.Right(1).bind()
* val y = Validated.Valid(2).bind()
* val z =
* attempt { None.bind { "Option was empty" } } catch { 0 }
* x + y + z
* }.fold({ fail("Shift can never be the result") }, { it shouldBe 3 })
* }
* ```
* <!--- KNIT example-eager-effect-scope-08.kt -->
*/
public infix fun <E, A> (suspend EagerEffectScope<E>.() -> A).catch(
recover: EagerEffectScope<R>.(E) -> A,
): A = eagerEffect(this).fold({ recover(it) }, ::identity)
}
/**
* Ensure that [value] is not `null`. if it's non-null it will be smart-casted and returned if it's
* `false` it will `shift` with the provided value [R]. Monadic version of [kotlin.requireNotNull].
*
* ```kotlin
* import arrow.core.continuations.eagerEffect
* import arrow.core.continuations.ensureNotNull
* import arrow.core.left
* import arrow.core.right
* import io.kotest.matchers.shouldBe
*
* fun main() {
* val failure = "failed"
* val int: Int? = null
* eagerEffect<String, Int> {
* ensureNotNull(int) { failure }
* }.toEither() shouldBe (int?.right() ?: failure.left())
* }
* ```
* <!--- KNIT example-eager-effect-scope-09.kt -->
*/
@Deprecated(
"Use the arrow.core.raise.Raise type instead, which is more general and can be used to and can be used to raise typed errors or _logical failures_\n" +
"The Raise<R> type is source compatible, a simple find & replace of arrow.core.continuations.* to arrow.core.raise.* will do the trick.",
ReplaceWith("ensureNotNull(value, shift)", "arrow.core.raise.ensureNotNull")
)
@OptIn(ExperimentalContracts::class)
public suspend fun <R, B : Any> EagerEffectScope<R>.ensureNotNull(value: B?, shift: () -> R): B {
contract { returns() implies (value != null) }
return value ?: shift(shift())
}