Skip to content

Commit f013aba

Browse files
committed
Add choose and rename Choice to Choose
1 parent ed66ec0 commit f013aba

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

library/src/jvmMain/kotlin/dsl.kt

+15-10
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,53 @@ private class PromptFail<R>(private val prompt: Prompt<R>, private val failValue
1010
override fun raise(e: Unit): Nothing = prompt.abort(failValue)
1111
}
1212

13-
public typealias Choice = Prompt<Unit>
13+
public typealias Choose = Prompt<Unit>
1414

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) {
1616
pushPrompt {
1717
handler(body())
1818
}
1919
}
2020

2121
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
2323
) {
2424
val prompt = Prompt<Unit>()
2525
prompt.pushChoice({
2626
body(SingletonRaise(PromptFail(prompt, Unit)), prompt)
2727
}, handler)
2828
}
2929

30-
public suspend fun <R> Choice.pushList(body: suspend () -> R): List<R> =
30+
public suspend fun <R> Choose.pushList(body: suspend () -> R): List<R> =
3131
runForkingReader(mutableListOf(), MutableList<R>::toMutableList) {
3232
pushChoice(body) {
3333
ask().add(it)
3434
}
3535
ask()
3636
}
3737

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> =
3939
runForkingReader(mutableListOf(), MutableList<R>::toMutableList) {
4040
runChoice(body) {
4141
ask().add(it)
4242
}
4343
ask()
4444
}
4545

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> =
4747
runCC { runList(body) }
4848

49-
context(Choice)
49+
context(Choose)
5050
public suspend fun <T> List<T>.bind(): T = shift { continuation ->
5151
for (item in 0..lastIndex) continuation(this[item])
5252
}
5353

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)
5560
public suspend fun IntRange.bind(): Int = shift { continuation ->
5661
for (i in start..endInclusive) continuation(i)
5762
}
@@ -61,14 +66,14 @@ public suspend fun <T> replicate(amount: Int, producer: suspend (Int) -> T): Lis
6166
}
6267

6368
public fun <R> flowReset(
64-
body: suspend context(SingletonRaise<Unit>, Choice) () -> R
69+
body: suspend context(SingletonRaise<Unit>, Choose) () -> R
6570
): Flow<R> = channelFlow {
6671
runCC {
6772
runChoice(body, this::send)
6873
}
6974
}
7075

71-
context(Choice)
76+
context(Choose)
7277
@OptIn(ExperimentalCoroutinesApi::class)
7378
public suspend fun <T> Flow<T>.bind(): T = shift { continuation ->
7479
// TODO using coroutineScope in such a way is generally unsafe unless a nonReentrant block is used

library/src/jvmTest/kotlin/ListTest.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -127,22 +127,20 @@ class ListTest {
127127

128128
@Test
129129
fun allEightBitPatterns() = runTest {
130-
val bits = listOf(0, 1)
131130
val result = listReset {
132131
replicate(8) {
133-
bits.bind()
132+
choose(0, 1)
134133
}
135134
}
136135
result.map { it.joinToString("").toInt(2) } shouldBe (0..255).toList()
137136
}
138137

139138
@Test
140139
fun allEightBitPatternsWithOnlyChange() = runTest {
141-
val bits = listOf(0, 1)
142140
val result = buildString {
143141
listReset {
144142
repeat(8) {
145-
append(bits.bind())
143+
append(choose(0, 1))
146144
}
147145
appendLine()
148146
}

0 commit comments

Comments
 (0)