@@ -10,48 +10,53 @@ private class PromptFail<R>(private val prompt: Prompt<R>, private val failValue
10
10
override fun raise (e : Unit ): Nothing = prompt.abort(failValue)
11
11
}
12
12
13
- public typealias Choice = Prompt <Unit >
13
+ public typealias Choose = Prompt <Unit >
14
14
15
- public suspend fun <R > Choice .pushChoice (body : suspend () -> R , handler : suspend (R ) -> Unit ) {
15
+ public suspend fun <R > Choose .pushChoice (body : suspend () -> R , handler : suspend (R ) -> Unit ) {
16
16
pushPrompt {
17
17
handler(body())
18
18
}
19
19
}
20
20
21
21
public suspend fun <R > runChoice (
22
- body : suspend context(SingletonRaise <Unit >, Choice ) () -> R , handler : suspend (R ) -> Unit
22
+ body : suspend context(SingletonRaise <Unit >, Choose ) () -> R , handler : suspend (R ) -> Unit
23
23
) {
24
24
val prompt = Prompt <Unit >()
25
25
prompt.pushChoice({
26
26
body(SingletonRaise (PromptFail (prompt, Unit )), prompt)
27
27
}, handler)
28
28
}
29
29
30
- public suspend fun <R > Choice .pushList (body : suspend () -> R ): List <R > =
30
+ public suspend fun <R > Choose .pushList (body : suspend () -> R ): List <R > =
31
31
runForkingReader(mutableListOf (), MutableList <R >::toMutableList) {
32
32
pushChoice(body) {
33
33
ask().add(it)
34
34
}
35
35
ask()
36
36
}
37
37
38
- public suspend fun <R > runList (body : suspend context(SingletonRaise <Unit >, Choice ) () -> R ): List <R > =
38
+ public suspend fun <R > runList (body : suspend context(SingletonRaise <Unit >, Choose ) () -> R ): List <R > =
39
39
runForkingReader(mutableListOf (), MutableList <R >::toMutableList) {
40
40
runChoice(body) {
41
41
ask().add(it)
42
42
}
43
43
ask()
44
44
}
45
45
46
- public suspend fun <R > listReset (body : suspend context(SingletonRaise <Unit >, Choice ) () -> R ): List <R > =
46
+ public suspend fun <R > listReset (body : suspend context(SingletonRaise <Unit >, Choose ) () -> R ): List <R > =
47
47
runCC { runList(body) }
48
48
49
- context(Choice )
49
+ context(Choose )
50
50
public suspend fun <T > List<T>.bind (): T = shift { continuation ->
51
51
for (item in 0 .. lastIndex) continuation(this [item])
52
52
}
53
53
54
- context(Choice )
54
+ public suspend fun <T > Choose.choose (left : T , right : T ): T = shift { continuation ->
55
+ continuation(left)
56
+ continuation(right)
57
+ }
58
+
59
+ context(Choose )
55
60
public suspend fun IntRange.bind (): Int = shift { continuation ->
56
61
for (i in start.. endInclusive) continuation(i)
57
62
}
@@ -61,14 +66,14 @@ public suspend fun <T> replicate(amount: Int, producer: suspend (Int) -> T): Lis
61
66
}
62
67
63
68
public fun <R > flowReset (
64
- body : suspend context(SingletonRaise <Unit >, Choice ) () -> R
69
+ body : suspend context(SingletonRaise <Unit >, Choose ) () -> R
65
70
): Flow <R > = channelFlow {
66
71
runCC {
67
72
runChoice(body, this ::send)
68
73
}
69
74
}
70
75
71
- context(Choice )
76
+ context(Choose )
72
77
@OptIn(ExperimentalCoroutinesApi ::class )
73
78
public suspend fun <T > Flow<T>.bind (): T = shift { continuation ->
74
79
// TODO using coroutineScope in such a way is generally unsafe unless a nonReentrant block is used
0 commit comments