Skip to content

Commit e961a35

Browse files
authored
feat(dsl): generalize exchanging (#850)
1 parent 82f3a90 commit e961a35

File tree

5 files changed

+22
-22
lines changed

5 files changed

+22
-22
lines changed

dsl/src/commonMain/kotlin/it/unibo/collektive/aggregate/api/Aggregate.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ package it.unibo.collektive.aggregate.api
1111
import it.unibo.collektive.field.Field
1212
import kotlinx.serialization.serializer
1313

14-
typealias YieldingScope<Initial, Return> = YieldingContext<Initial, Return>.(Initial) -> YieldingResult<Initial, Return>
14+
typealias YieldingScope<Shared, Returned> =
15+
YieldingContext<Shared, Returned>.(Shared) -> YieldingResult<Shared, Returned>
1516

1617
/**
1718
* Represents methods intended to be used internally only.
@@ -49,8 +50,8 @@ interface Aggregate<ID : Any> {
4950
fun <Shared, Returned> InternalAPI.`_ serialization aware exchanging`(
5051
initial: Shared,
5152
dataSharingMethod: DataSharingMethod<Shared>,
52-
body: YieldingScope<Field<ID, Shared>, Field<ID, Returned>>,
53-
): Field<ID, Returned>
53+
body: YieldingScope<Field<ID, Shared>, Returned>,
54+
): Returned
5455

5556
/**
5657
* Iteratively updates the value computing the [transform] expression at each device using the last

dsl/src/commonMain/kotlin/it/unibo/collektive/aggregate/api/CoreOperators.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ import it.unibo.collektive.field.Field
3434
*/
3535
inline fun <ID : Any, reified Shared, Returned> Aggregate<ID>.exchanging(
3636
initial: Shared,
37-
noinline body: YieldingScope<Field<ID, Shared>, Field<ID, Returned>>,
38-
): Field<ID, Returned> =
37+
noinline body: YieldingScope<Field<ID, Shared>, Returned>,
38+
): Returned =
3939
@OptIn(DelicateCollektiveApi::class)
4040
InternalAPI.`_ serialization aware exchanging`(initial, dataSharingMethod(), body)
4141

dsl/src/commonMain/kotlin/it/unibo/collektive/aggregate/api/Operators.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import it.unibo.collektive.field.Field
3131
inline fun <ID : Any, reified Shared> Aggregate<ID>.exchange(
3232
initial: Shared,
3333
noinline body: (Field<ID, Shared>) -> Field<ID, Shared>,
34-
): Field<ID, Shared> = exchanging(initial) { field -> body(field).run { yielding { this } } }
34+
): Field<ID, Shared> = exchanging(initial) { field -> body(field).yielding { this } }
3535

3636
/**
3737
* [share] implements efficient stateful data sharing.
@@ -48,7 +48,7 @@ inline fun <ID : Any, reified Shared> Aggregate<ID>.exchange(
4848
inline fun <ID : Any, reified Shared> Aggregate<ID>.share(
4949
initial: Shared,
5050
noinline body: (Field<ID, Shared>) -> Shared,
51-
): Shared = sharing(initial) { field -> body(field).run { yielding { this } } }
51+
): Shared = sharing(initial) { field -> body(field).yielding { this } }
5252

5353
/**
5454
* [sharing] implements efficient stateful data sharing.
@@ -67,8 +67,6 @@ inline fun <ID : Any, reified Shared, Returned> Aggregate<ID>.sharing(
6767
): Returned = exchanging(initial) { field: Field<ID, Shared> ->
6868
with(YieldingContext<Shared, Returned>()) {
6969
val result: YieldingResult<Shared, Returned> = body(field)
70-
field.map { result.toSend }.yielding {
71-
field.map { result.toReturn }
72-
}
70+
field.mapToConstantField(result.toSend).yielding { result.toReturn }
7371
}
74-
}.localValue
72+
}

dsl/src/commonMain/kotlin/it/unibo/collektive/aggregate/api/YieldSupport.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class YieldingContext<Shared, Returned> {
1717
/**
1818
* Computes [toReturn] after the data exchange operation is complete.
1919
*/
20-
fun Shared.yielding(toReturn: () -> Returned): YieldingResult<Shared, Returned> = YieldingResult(this, toReturn())
20+
fun Shared.yielding(toReturn: Shared.() -> Returned): YieldingResult<Shared, Returned> =
21+
YieldingResult(this, this.toReturn())
2122
}
2223

2324
/**

dsl/src/commonMain/kotlin/it/unibo/collektive/aggregate/api/impl/AggregateContext.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,22 @@ internal class AggregateContext<ID : Any>(
5656
private fun <T> newField(localValue: T, others: Map<ID, T>): Field<ID, T> = Field(localId, localValue, others)
5757

5858
@DelicateCollektiveApi
59-
override fun <Initial, Ret> InternalAPI.`_ serialization aware exchanging`(
60-
initial: Initial,
61-
dataSharingMethod: DataSharingMethod<Initial>,
62-
body: YieldingScope<Field<ID, Initial>, Field<ID, Ret>>,
63-
): Field<ID, Ret> {
59+
override fun <Shared, Returned> InternalAPI.`_ serialization aware exchanging`(
60+
initial: Shared,
61+
dataSharingMethod: DataSharingMethod<Shared>,
62+
body: YieldingScope<Field<ID, Shared>, Returned>,
63+
): Returned {
6464
val path: Path = stack.currentPath()
65-
val messages = inboundMessage.dataAt<Initial>(path, dataSharingMethod)
65+
val messages = inboundMessage.dataAt<Shared>(path, dataSharingMethod)
6666
val previous = stateAt(path, initial)
67-
val subject = newField(previous, messages)
68-
val context = YieldingContext<Field<ID, Initial>, Field<ID, Ret>>()
69-
return body(context, subject)
67+
val subject: Field<ID, Shared> = newField(previous, messages)
68+
val context = YieldingContext<Field<ID, Shared>, Returned>()
69+
return context.body(subject)
7070
.also {
7171
val message = SharedData(
7272
it.toSend.localValue,
7373
when (it.toSend) {
74-
is ConstantField<ID, Initial> -> emptyMap()
74+
is ConstantField<ID, Shared> -> emptyMap()
7575
else -> it.toSend.excludeSelf()
7676
},
7777
)

0 commit comments

Comments
 (0)