diff --git a/arrow-libs/fx/arrow-fx-stm/api/arrow-fx-stm.api b/arrow-libs/fx/arrow-fx-stm/api/arrow-fx-stm.api index 3e6770ca1b5..e5a1ed57e45 100644 --- a/arrow-libs/fx/arrow-fx-stm/api/arrow-fx-stm.api +++ b/arrow-libs/fx/arrow-fx-stm/api/arrow-fx-stm.api @@ -7,6 +7,8 @@ public abstract interface class arrow/fx/stm/STM { public abstract fun fold (Larrow/fx/stm/TArray;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; public abstract fun get (Larrow/fx/stm/TArray;I)Ljava/lang/Object; public abstract fun get (Larrow/fx/stm/TMap;Ljava/lang/Object;)Ljava/lang/Object; + public abstract fun getValue (Larrow/fx/stm/TVar;)Ljava/lang/Object; + public abstract fun getValue (Larrow/fx/stm/TVar;Ljava/lang/Void;Lkotlin/reflect/KProperty;)Ljava/lang/Object; public abstract fun insert (Larrow/fx/stm/TMap;Ljava/lang/Object;Ljava/lang/Object;)V public abstract fun insert (Larrow/fx/stm/TSet;Ljava/lang/Object;)V public abstract fun isEmpty (Larrow/fx/stm/TMVar;)Z @@ -35,6 +37,8 @@ public abstract interface class arrow/fx/stm/STM { public abstract fun retry ()Ljava/lang/Void; public abstract fun set (Larrow/fx/stm/TArray;ILjava/lang/Object;)V public abstract fun set (Larrow/fx/stm/TMap;Ljava/lang/Object;Ljava/lang/Object;)V + public abstract fun set (Larrow/fx/stm/TVar;Ljava/lang/Object;)V + public abstract fun setValue (Larrow/fx/stm/TVar;Ljava/lang/Void;Lkotlin/reflect/KProperty;Ljava/lang/Object;)V public abstract fun size (Larrow/fx/stm/TQueue;)I public abstract fun swap (Larrow/fx/stm/TMVar;Ljava/lang/Object;)Ljava/lang/Object; public abstract fun swap (Larrow/fx/stm/TVar;Ljava/lang/Object;)Ljava/lang/Object; @@ -61,6 +65,8 @@ public final class arrow/fx/stm/STM$DefaultImpls { public static fun fold (Larrow/fx/stm/STM;Larrow/fx/stm/TArray;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; public static fun get (Larrow/fx/stm/STM;Larrow/fx/stm/TArray;I)Ljava/lang/Object; public static fun get (Larrow/fx/stm/STM;Larrow/fx/stm/TMap;Ljava/lang/Object;)Ljava/lang/Object; + public static fun getValue (Larrow/fx/stm/STM;Larrow/fx/stm/TVar;)Ljava/lang/Object; + public static fun getValue (Larrow/fx/stm/STM;Larrow/fx/stm/TVar;Ljava/lang/Void;Lkotlin/reflect/KProperty;)Ljava/lang/Object; public static fun insert (Larrow/fx/stm/STM;Larrow/fx/stm/TMap;Ljava/lang/Object;Ljava/lang/Object;)V public static fun insert (Larrow/fx/stm/STM;Larrow/fx/stm/TSet;Ljava/lang/Object;)V public static fun isEmpty (Larrow/fx/stm/STM;Larrow/fx/stm/TMVar;)Z @@ -86,6 +92,8 @@ public final class arrow/fx/stm/STM$DefaultImpls { public static fun removeAll (Larrow/fx/stm/STM;Larrow/fx/stm/TQueue;Lkotlin/jvm/functions/Function1;)V public static fun set (Larrow/fx/stm/STM;Larrow/fx/stm/TArray;ILjava/lang/Object;)V public static fun set (Larrow/fx/stm/STM;Larrow/fx/stm/TMap;Ljava/lang/Object;Ljava/lang/Object;)V + public static fun set (Larrow/fx/stm/STM;Larrow/fx/stm/TVar;Ljava/lang/Object;)V + public static fun setValue (Larrow/fx/stm/STM;Larrow/fx/stm/TVar;Ljava/lang/Void;Lkotlin/reflect/KProperty;Ljava/lang/Object;)V public static fun size (Larrow/fx/stm/STM;Larrow/fx/stm/TQueue;)I public static fun swap (Larrow/fx/stm/STM;Larrow/fx/stm/TMVar;Ljava/lang/Object;)Ljava/lang/Object; public static fun swap (Larrow/fx/stm/STM;Larrow/fx/stm/TVar;Ljava/lang/Object;)Ljava/lang/Object; diff --git a/arrow-libs/fx/arrow-fx-stm/api/arrow-fx-stm.klib.api b/arrow-libs/fx/arrow-fx-stm/api/arrow-fx-stm.klib.api index 1012de630f5..6f612e7fc26 100644 --- a/arrow-libs/fx/arrow-fx-stm/api/arrow-fx-stm.klib.api +++ b/arrow-libs/fx/arrow-fx-stm/api/arrow-fx-stm.klib.api @@ -56,9 +56,14 @@ abstract interface arrow.fx.stm/STM { // arrow.fx.stm/STM|null[0] open fun <#A1: kotlin/Any?> (arrow.fx.stm/TSet<#A1>).member(#A1): kotlin/Boolean // arrow.fx.stm/STM.member|member@arrow.fx.stm.TSet<0:0>(0:0){0§}[0] open fun <#A1: kotlin/Any?> (arrow.fx.stm/TSet<#A1>).plusAssign(#A1) // arrow.fx.stm/STM.plusAssign|plusAssign@arrow.fx.stm.TSet<0:0>(0:0){0§}[0] open fun <#A1: kotlin/Any?> (arrow.fx.stm/TSet<#A1>).remove(#A1) // arrow.fx.stm/STM.remove|remove@arrow.fx.stm.TSet<0:0>(0:0){0§}[0] + open fun <#A1: kotlin/Any?> (arrow.fx.stm/TVar<#A1>).getValue(kotlin/Nothing?, kotlin.reflect/KProperty<*>): #A1 // arrow.fx.stm/STM.getValue|getValue@arrow.fx.stm.TVar<0:0>(kotlin.Nothing?;kotlin.reflect.KProperty<*>){0§}[0] open fun <#A1: kotlin/Any?> (arrow.fx.stm/TVar<#A1>).modify(kotlin/Function1<#A1, #A1>) // arrow.fx.stm/STM.modify|modify@arrow.fx.stm.TVar<0:0>(kotlin.Function1<0:0,0:0>){0§}[0] + open fun <#A1: kotlin/Any?> (arrow.fx.stm/TVar<#A1>).set(#A1) // arrow.fx.stm/STM.set|set@arrow.fx.stm.TVar<0:0>(0:0){0§}[0] + open fun <#A1: kotlin/Any?> (arrow.fx.stm/TVar<#A1>).setValue(kotlin/Nothing?, kotlin.reflect/KProperty<*>, #A1) // arrow.fx.stm/STM.setValue|setValue@arrow.fx.stm.TVar<0:0>(kotlin.Nothing?;kotlin.reflect.KProperty<*>;0:0){0§}[0] open fun <#A1: kotlin/Any?> (arrow.fx.stm/TVar<#A1>).swap(#A1): #A1 // arrow.fx.stm/STM.swap|swap@arrow.fx.stm.TVar<0:0>(0:0){0§}[0] open fun <#A1: kotlin/Any?> newTVar(#A1): arrow.fx.stm/TVar<#A1> // arrow.fx.stm/STM.newTVar|newTVar(0:0){0§}[0] + open val value // arrow.fx.stm/STM.value|@arrow.fx.stm.TVar<0:0>{0§}value[0] + open fun <#A2: kotlin/Any?> (arrow.fx.stm/TVar<#A2>).(): #A2 // arrow.fx.stm/STM.value.|@arrow.fx.stm.TVar<0:0>(){0§}[0] } final class <#A: kotlin/Any?, #B: kotlin/Any?> arrow.fx.stm/TMap { // arrow.fx.stm/TMap|null[0] final fun copy(arrow.fx.stm.internal/Hamt> = ..., kotlin/Function1<#A, kotlin/Int> = ...): arrow.fx.stm/TMap<#A, #B> // arrow.fx.stm/TMap.copy|copy(arrow.fx.stm.internal.Hamt>;kotlin.Function1<1:0,kotlin.Int>){}[0] diff --git a/arrow-libs/fx/arrow-fx-stm/src/commonMain/kotlin/arrow/fx/stm/STM.kt b/arrow-libs/fx/arrow-fx-stm/src/commonMain/kotlin/arrow/fx/stm/STM.kt index 5e4439a0509..9f80b08a98e 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/commonMain/kotlin/arrow/fx/stm/STM.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/commonMain/kotlin/arrow/fx/stm/STM.kt @@ -3,6 +3,7 @@ package arrow.fx.stm import arrow.fx.stm.internal.STMTransaction import arrow.fx.stm.internal.alterHamtWithHash import arrow.fx.stm.internal.lookupHamtWithHash +import kotlin.reflect.KProperty /** * # Consistent and safe concurrent state updates @@ -286,6 +287,32 @@ public interface STM { */ public fun TVar.read(): A + /** + * Read the value from a [TVar]. + * + * ```kotlin + * import arrow.fx.stm.TVar + * import arrow.fx.stm.atomically + * + * suspend fun main() { + * //sampleStart + * val tvar = TVar.new(10) + * val result = atomically { + * tvar.value + * } + * //sampleEnd + * println(result) + * } + * ``` + * + * + * This comes with a few guarantees: + * - Any given [TVar] is only ever read once during a transaction. + * - When committing the transaction the value read has to be equal to the current value otherwise the + * transaction will retry + */ + public val TVar.value: A get() = read() + /** * Set the value of a [TVar]. * @@ -303,7 +330,7 @@ public interface STM { * println(result) * } * ``` - * + * * * Similarly to [read] this comes with a few guarantees: * - For multiple writes to the same [TVar] in a transaction only the last will actually be performed @@ -312,6 +339,78 @@ public interface STM { */ public fun TVar.write(a: A) + /** + * Set the value of a [TVar]. + * + * ```kotlin + * import arrow.fx.stm.TVar + * import arrow.fx.stm.atomically + * + * suspend fun main() { + * //sampleStart + * val tvar = TVar.new(10) + * val result = atomically { + * tvar.set(20) + * } + * //sampleEnd + * println(result) + * } + * ``` + * + * + * Similarly to [value] this comes with a few guarantees: + * - For multiple writes to the same [TVar] in a transaction only the last will actually be performed + * - When committing the value inside the [TVar], at the time of calling [set], has to be the + * same as the current value otherwise the transaction will retry + */ + public fun TVar.set(a: A) { write(a) } + + /** + * Use a [TVar] using property delegation. + * + * ```kotlin + * import arrow.fx.stm.TVar + * import arrow.fx.stm.atomically + * + * suspend fun main() { + * //sampleStart + * val tvar = TVar.new(10) + * val result = atomically { + * val x by tvar + * x + * } + * //sampleEnd + * println(result) + * } + * ``` + * + * + */ + public operator fun TVar.getValue(thisRef: Nothing?, property: KProperty<*>): A = read() + + /** + * Use a [TVar] using property delegation. + * + * ```kotlin + * import arrow.fx.stm.TVar + * import arrow.fx.stm.atomically + * + * suspend fun main() { + * //sampleStart + * val tvar = TVar.new(10) + * val result = atomically { + * var x by tvar + * x = 20 + * } + * //sampleEnd + * println(result) + * } + * ``` + * + * + */ + public operator fun TVar.setValue(thisRef: Nothing?, property: KProperty<*>, value: A) { write(value) } + /** * Modify the value of a [TVar] * @@ -329,7 +428,7 @@ public interface STM { * println(result) * } * ``` - * + * * * `modify(f) = write(f(read()))` */ @@ -353,7 +452,7 @@ public interface STM { * println("New value ${tvar.unsafeRead()}") * } * ``` - * + * * * @return The previous value stored inside the [TVar] */ @@ -383,7 +482,7 @@ public interface STM { * println("New value ${atomically { tmvar.tryTake() } }") * } * ``` - * + * * * This retries if the [TMVar] is empty and leaves the [TMVar] empty if it succeeded. * @@ -412,7 +511,7 @@ public interface STM { * println("New value ${atomically { tmvar.tryTake() } }") * } * ``` - * + * * * This retries if the [TMVar] is not empty. * @@ -441,7 +540,7 @@ public interface STM { * println("New value ${atomically { tmvar.tryTake() } }") * } * ``` - * + * * * This retries if the [TMVar] is empty but does not take the value out if it succeeds. * @@ -471,7 +570,7 @@ public interface STM { * println("New value ${atomically { tmvar.tryTake() } }") * } * ``` - * + * */ public fun TMVar.tryTake(): A? = when (val ret = v.read()) { is Option.Some -> ret.a.also { v.write(Option.None) } @@ -496,7 +595,7 @@ public interface STM { * println("New value ${atomically { tmvar.tryTake() } }") * } * ``` - * + * * * This function never retries. * @@ -524,7 +623,7 @@ public interface STM { * println("Result $result") * } * ``` - * + * * * @see TMVar.read for a function that retries if the [TMVar] is empty. * @see TMVar.tryTake for a function that leaves the [TMVar] empty after reading. @@ -551,7 +650,7 @@ public interface STM { * println("Result $result") * } * ``` - * + * * * > Because the state of a transaction is constant there can never be a race condition between checking if a `TMVar` is empty and subsequent * reads in the *same* transaction. @@ -575,7 +674,7 @@ public interface STM { * println("Result $result") * } * ``` - * + * * * > Because the state of a transaction is constant there can never be a race condition between checking if a `TMVar` is empty and subsequent * reads in the *same* transaction. @@ -601,7 +700,7 @@ public interface STM { * println("New value ${atomically { tmvar.tryTake() } }") * } * ``` - * + * */ public fun TMVar.swap(a: A): A = when (val ret = v.read()) { is Option.Some -> ret.a.also { v.write(Option.Some(a)) } @@ -627,7 +726,7 @@ public interface STM { * println("Permits remaining ${atomically { tsem.available() }}") * } * ``` - * + * * * This function never retries. */ @@ -651,7 +750,7 @@ public interface STM { * println("Permits remaining ${atomically { tsem.available() }}") * } * ``` - * + * * * This function will retry if there are no permits available. * @@ -677,7 +776,7 @@ public interface STM { * println("Permits remaining ${atomically { tsem.available() }}") * } * ``` - * + * * * This function will retry if there are less than [n] permits available. * @@ -707,7 +806,7 @@ public interface STM { * println("Permits remaining ${atomically { tsem.available() }}") * } * ``` - * + * * * This function never retries. * @@ -734,7 +833,7 @@ public interface STM { * println("Permits remaining ${atomically { tsem.available() }}") * } * ``` - * + * * * This function never retries. * @@ -760,7 +859,7 @@ public interface STM { * println("Permits remaining ${atomically { tsem.available() }}") * } * ``` - * + * * * This function never retries. */ @@ -784,7 +883,7 @@ public interface STM { * println("Permits remaining ${atomically { tsem.available() }}") * } * ``` - * + * * * [n] must be non-negative. * @@ -816,7 +915,7 @@ public interface STM { * println("Items in queue ${atomically { tq.flush() }}") * } * ``` - * + * * * This function never retries. */ @@ -840,7 +939,7 @@ public interface STM { * println("Items in queue ${atomically { tq.flush() }}") * } * ``` - * + * * * This function never retries. */ @@ -865,7 +964,7 @@ public interface STM { * println("Items in queue ${atomically { tq.flush() }}") * } * ``` - * + * * * @see TQueue.tryRead for a version that does not retry. * @see TQueue.peek for a version that does not remove the element. @@ -903,7 +1002,7 @@ public interface STM { * println("Items in queue ${atomically { tq.flush() }}") * } * ``` - * + * * * This function never retries. */ @@ -931,7 +1030,7 @@ public interface STM { * println("Items in queue ${atomically { tq.flush() }}") * } * ``` - * + * * * This function never retries. */ @@ -961,7 +1060,7 @@ public interface STM { * println("Items in queue ${atomically { tq.flush() }}") * } * ``` - * + * * * This function retries if the [TQueue] is empty. * @@ -989,7 +1088,7 @@ public interface STM { * println("Items in queue ${atomically { tq.flush() }}") * } * ``` - * + * * * This function never retries. * @@ -1017,7 +1116,7 @@ public interface STM { * println("Items in queue ${atomically { tq.flush() }}") * } * ``` - * + * * * Mainly used to implement [TQueue.peek] and since this writes to the read variable of a [TQueue] excessive use * can lead to contention on consumers. Prefer appending to a [TQueue] if possible. @@ -1044,7 +1143,7 @@ public interface STM { * println("Result $result") * } * ``` - * + * * * This function never retries. * @@ -1070,7 +1169,7 @@ public interface STM { * println("Result $result") * } * ``` - * + * * * This function never retries. * @@ -1097,7 +1196,7 @@ public interface STM { * println("Items in queue ${atomically { tq.flush() }}") * } * ``` - * + * * * This function never retries. * @@ -1125,7 +1224,7 @@ public interface STM { * println("Result $result") * } * ``` - * + * * * This function never retries. * @@ -1151,7 +1250,7 @@ public interface STM { * println("Result $result") * } * ``` - * + * * * Throws if [i] is out of bounds. * @@ -1179,7 +1278,7 @@ public interface STM { * println("Result $result") * } * ``` - * + * * * Throws if [i] is out of bounds. * @@ -1205,7 +1304,7 @@ public interface STM { * println("Result $result") * } * ``` - * + * * * This function never retries. */ @@ -1229,7 +1328,7 @@ public interface STM { * println("Result $result") * } * ``` - * + * * * This function never retries. */ @@ -1255,7 +1354,7 @@ public interface STM { * //sampleEnd * } * ``` - * + * * * This function never retries. */ @@ -1282,7 +1381,7 @@ public interface STM { * println("Result $result") * } * ``` - * + * * * > If the key is not present [STM.lookup] will not retry, instead it returns `null`. */ @@ -1309,7 +1408,7 @@ public interface STM { * println("Result $result") * } * ``` - * + * * * > If the key is not present [STM.get] will not retry, instead it returns `null`. */ @@ -1331,7 +1430,7 @@ public interface STM { * //sampleEnd * } * ``` - * + * */ public fun TMap.insert(k: K, v: V) { alterHamtWithHash(hamt, hashFn(k), { it.first == k }) { k to v } @@ -1353,7 +1452,7 @@ public interface STM { * //sampleEnd * } * ``` - * + * */ public operator fun TMap.set(k: K, v: V): Unit = insert(k, v) @@ -1373,7 +1472,7 @@ public interface STM { * //sampleEnd * } * ``` - * + * */ public operator fun TMap.plusAssign(kv: Pair): Unit = insert(kv.first, kv.second) @@ -1396,7 +1495,7 @@ public interface STM { * println("Result $result") * } * ``` - * + * */ public fun TMap.update(k: K, fn: (V) -> V) { alterHamtWithHash(hamt, hashFn(k), { it.first == k }) { it?.second?.let(fn)?.let { r -> k to r } } @@ -1419,7 +1518,7 @@ public interface STM { * //sampleEnd * } * ``` - * + * */ public fun TMap.remove(k: K) { alterHamtWithHash(hamt, hashFn(k), { it.first == k }) { null } @@ -1444,7 +1543,7 @@ public interface STM { * println("Result $result") * } * ``` - * + * */ public fun TSet.member(a: A): Boolean = lookupHamtWithHash(hamt, hashFn(a)) { it == a } != null @@ -1465,7 +1564,7 @@ public interface STM { * //sampleEnd * } * ``` - * + * */ public fun TSet.insert(a: A) { alterHamtWithHash(hamt, hashFn(a), { it == a }) { a } @@ -1487,7 +1586,7 @@ public interface STM { * //sampleEnd * } * ``` - * + * */ public operator fun TSet.plusAssign(a: A): Unit = insert(a) @@ -1508,7 +1607,7 @@ public interface STM { * //sampleEnd * } * ``` - * + * */ public fun TSet.remove(a: A) { alterHamtWithHash(hamt, hashFn(a), { it == a }) { null } @@ -1535,7 +1634,7 @@ public interface STM { * println("Result $result") * } * ``` - * + * * * Equal to [suspend] just with an [STM] receiver. */ @@ -1562,7 +1661,7 @@ public inline fun stm(noinline f: STM.() -> A): STM.() -> A = f * println("Result $result") * } * ``` - * + * * * `check(b) = if (b.not()) retry() else Unit` */ diff --git a/arrow-libs/fx/arrow-fx-stm/src/commonMain/kotlin/arrow/fx/stm/TQueue.kt b/arrow-libs/fx/arrow-fx-stm/src/commonMain/kotlin/arrow/fx/stm/TQueue.kt index 70679af620f..8eb227c045d 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/commonMain/kotlin/arrow/fx/stm/TQueue.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/commonMain/kotlin/arrow/fx/stm/TQueue.kt @@ -260,8 +260,8 @@ public data class TQueue internal constructor( * implement queues. */ internal sealed class PList { - public data class Cons(val value: A, @JsName("_tail") val tail: PList) : PList() - object Nil : PList() + data class Cons(val value: A, @JsName("_tail") val tail: PList) : PList() + data object Nil : PList() /** * O(1) @@ -290,47 +290,46 @@ internal sealed class PList { fun isNotEmpty(): Boolean = this != Nil /** - * O(n) and the entire list is copied + * Execute an action on every element */ - fun reverse(): PList { - var new: PList = Nil + private inline fun forEach(crossinline action: (A) -> Unit) { var xs = this while (xs is Cons) { - new = Cons(xs.value, new) + action(xs.value) xs = xs.tail } + } + + /** + * O(n) and the entire list is copied + */ + fun reverse(): PList { + var new: PList = Nil + this.forEach { new = Cons(it, new) } return new } /** * O(n) */ - inline fun filter(pred: Predicate): PList { - var new: PList = Nil - var xs = this - while (xs is Cons) { - if (pred(xs.value)) new = Cons(xs.value, new) - xs = xs.tail + fun filter(pred: Predicate): PList { + var chosen: PList = Nil + this.forEach { + if (pred(it)) chosen = Cons(it, chosen) } - return new.reverse() + return chosen.reverse() } /** * O(n) */ - inline fun filterNot(pred: Predicate): PList = filter { pred(it).not() } + // fun filterNot(pred: Predicate): PList = filter { pred(it).not() } /** * O(n) */ - fun toList(): List { - val mutList = mutableListOf() - var xs = this - while (xs is Cons) { - mutList.add(xs.value) - xs = xs.tail - } - return mutList + fun toList(): List = buildList { + this@PList.forEach { add(it) } } /** @@ -338,11 +337,7 @@ internal sealed class PList { */ fun size(): Int { var sz = 0 - var xs = this - while (xs is Cons) { - sz += 1 - xs = xs.tail - } + this.forEach { sz++ } return sz } } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-08.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-08.kt index 3b244a95524..8248c5cc1ab 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-08.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-08.kt @@ -8,7 +8,7 @@ suspend fun main() { //sampleStart val tvar = TVar.new(10) val result = atomically { - tvar.write(20) + tvar.value } //sampleEnd println(result) diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-09.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-09.kt index 635fed77742..4a6a9f8f832 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-09.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-09.kt @@ -8,7 +8,7 @@ suspend fun main() { //sampleStart val tvar = TVar.new(10) val result = atomically { - tvar.modify { it * 2 } + tvar.write(20) } //sampleEnd println(result) diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-10.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-10.kt index d644d15ef6d..d000bcf0b65 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-10.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-10.kt @@ -8,9 +8,8 @@ suspend fun main() { //sampleStart val tvar = TVar.new(10) val result = atomically { - tvar.swap(20) + tvar.set(20) } //sampleEnd - println("Result $result") - println("New value ${tvar.unsafeRead()}") + println(result) } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-11.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-11.kt index b05a8118851..1ea552af084 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-11.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-11.kt @@ -1,16 +1,16 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm11 -import arrow.fx.stm.TMVar +import arrow.fx.stm.TVar import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tmvar = TMVar.new(10) + val tvar = TVar.new(10) val result = atomically { - tmvar.take() + val x by tvar + x } //sampleEnd - println("Result $result") - println("New value ${atomically { tmvar.tryTake() } }") + println(result) } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-12.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-12.kt index b2472302a9c..819e08951e3 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-12.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-12.kt @@ -1,15 +1,16 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm12 -import arrow.fx.stm.TMVar +import arrow.fx.stm.TVar import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tmvar = TMVar.empty() - atomically { - tmvar.put(20) + val tvar = TVar.new(10) + val result = atomically { + var x by tvar + x = 20 } //sampleEnd - println("New value ${atomically { tmvar.tryTake() } }") + println(result) } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-13.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-13.kt index 7e88db38857..0ce8410fa4b 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-13.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-13.kt @@ -1,16 +1,15 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm13 -import arrow.fx.stm.TMVar +import arrow.fx.stm.TVar import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tmvar = TMVar.new(30) + val tvar = TVar.new(10) val result = atomically { - tmvar.read() + tvar.modify { it * 2 } } //sampleEnd - println("Result $result") - println("New value ${atomically { tmvar.tryTake() } }") + println(result) } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-14.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-14.kt index 703b3f3c201..ad25b8f7e76 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-14.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-14.kt @@ -1,16 +1,16 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm14 -import arrow.fx.stm.TMVar +import arrow.fx.stm.TVar import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tmvar = TMVar.empty() + val tvar = TVar.new(10) val result = atomically { - tmvar.tryTake() + tvar.swap(20) } //sampleEnd println("Result $result") - println("New value ${atomically { tmvar.tryTake() } }") + println("New value ${tvar.unsafeRead()}") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-15.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-15.kt index 69e36bb351b..35ed5651169 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-15.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-15.kt @@ -6,9 +6,9 @@ import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tmvar = TMVar.new(20) + val tmvar = TMVar.new(10) val result = atomically { - tmvar.tryPut(30) + tmvar.take() } //sampleEnd println("Result $result") diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-16.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-16.kt index 10417ff327a..d44d2c4faf9 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-16.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-16.kt @@ -7,9 +7,9 @@ import arrow.fx.stm.atomically suspend fun main() { //sampleStart val tmvar = TMVar.empty() - val result = atomically { - tmvar.tryRead() + atomically { + tmvar.put(20) } //sampleEnd - println("Result $result") + println("New value ${atomically { tmvar.tryTake() } }") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-17.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-17.kt index 2aa661f6954..9a47b5a624e 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-17.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-17.kt @@ -6,10 +6,11 @@ import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tmvar = TMVar.empty() + val tmvar = TMVar.new(30) val result = atomically { - tmvar.isEmpty() + tmvar.read() } //sampleEnd println("Result $result") + println("New value ${atomically { tmvar.tryTake() } }") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-18.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-18.kt index d9f7e449086..18a0d01ded3 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-18.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-18.kt @@ -8,8 +8,9 @@ suspend fun main() { //sampleStart val tmvar = TMVar.empty() val result = atomically { - tmvar.isNotEmpty() + tmvar.tryTake() } //sampleEnd println("Result $result") + println("New value ${atomically { tmvar.tryTake() } }") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-19.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-19.kt index 79aa77c885a..ce051bda51b 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-19.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-19.kt @@ -6,9 +6,9 @@ import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tmvar = TMVar.new(30) + val tmvar = TMVar.new(20) val result = atomically { - tmvar.swap(40) + tmvar.tryPut(30) } //sampleEnd println("Result $result") diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-20.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-20.kt index 7943c27ad66..0b5d435e666 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-20.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-20.kt @@ -1,16 +1,15 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm20 -import arrow.fx.stm.TSemaphore +import arrow.fx.stm.TMVar import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tsem = TSemaphore.new(5) + val tmvar = TMVar.empty() val result = atomically { - tsem.available() + tmvar.tryRead() } //sampleEnd println("Result $result") - println("Permits remaining ${atomically { tsem.available() }}") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-21.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-21.kt index d4bab988624..4b161ab3ebb 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-21.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-21.kt @@ -1,15 +1,15 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm21 -import arrow.fx.stm.TSemaphore +import arrow.fx.stm.TMVar import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tsem = TSemaphore.new(5) - atomically { - tsem.acquire() + val tmvar = TMVar.empty() + val result = atomically { + tmvar.isEmpty() } //sampleEnd - println("Permits remaining ${atomically { tsem.available() }}") + println("Result $result") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-22.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-22.kt index 72df59804f7..c75592f75f6 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-22.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-22.kt @@ -1,15 +1,15 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm22 -import arrow.fx.stm.TSemaphore +import arrow.fx.stm.TMVar import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tsem = TSemaphore.new(5) - atomically { - tsem.acquire(3) + val tmvar = TMVar.empty() + val result = atomically { + tmvar.isNotEmpty() } //sampleEnd - println("Permits remaining ${atomically { tsem.available() }}") + println("Result $result") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-23.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-23.kt index e3e870700e3..e016e9126e0 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-23.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-23.kt @@ -1,16 +1,16 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm23 -import arrow.fx.stm.TSemaphore +import arrow.fx.stm.TMVar import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tsem = TSemaphore.new(0) + val tmvar = TMVar.new(30) val result = atomically { - tsem.tryAcquire() + tmvar.swap(40) } //sampleEnd println("Result $result") - println("Permits remaining ${atomically { tsem.available() }}") + println("New value ${atomically { tmvar.tryTake() } }") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-24.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-24.kt index d35101b9e6e..8316360626d 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-24.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-24.kt @@ -6,9 +6,9 @@ import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tsem = TSemaphore.new(0) + val tsem = TSemaphore.new(5) val result = atomically { - tsem.tryAcquire(3) + tsem.available() } //sampleEnd println("Result $result") diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-25.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-25.kt index 3ff16ef47e2..70079d3965b 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-25.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-25.kt @@ -8,7 +8,7 @@ suspend fun main() { //sampleStart val tsem = TSemaphore.new(5) atomically { - tsem.release() + tsem.acquire() } //sampleEnd println("Permits remaining ${atomically { tsem.available() }}") diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-26.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-26.kt index be83f122ebc..25de1187843 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-26.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-26.kt @@ -8,7 +8,7 @@ suspend fun main() { //sampleStart val tsem = TSemaphore.new(5) atomically { - tsem.release(2) + tsem.acquire(3) } //sampleEnd println("Permits remaining ${atomically { tsem.available() }}") diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-27.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-27.kt index ddb1d7ba8f5..1d580955813 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-27.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-27.kt @@ -1,15 +1,16 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm27 -import arrow.fx.stm.TQueue +import arrow.fx.stm.TSemaphore import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tq = TQueue.new() - atomically { - tq.write(2) + val tsem = TSemaphore.new(0) + val result = atomically { + tsem.tryAcquire() } //sampleEnd - println("Items in queue ${atomically { tq.flush() }}") + println("Result $result") + println("Permits remaining ${atomically { tsem.available() }}") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-28.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-28.kt index 5c7f3b4b29e..5f48395cc32 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-28.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-28.kt @@ -1,15 +1,16 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm28 -import arrow.fx.stm.TQueue +import arrow.fx.stm.TSemaphore import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tq = TQueue.new() - atomically { - tq += 2 + val tsem = TSemaphore.new(0) + val result = atomically { + tsem.tryAcquire(3) } //sampleEnd - println("Items in queue ${atomically { tq.flush() }}") + println("Result $result") + println("Permits remaining ${atomically { tsem.available() }}") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-29.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-29.kt index c6ffa1c7e4e..067c99a59b7 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-29.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-29.kt @@ -1,17 +1,15 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm29 -import arrow.fx.stm.TQueue +import arrow.fx.stm.TSemaphore import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tq = TQueue.new() - val result = atomically { - tq.write(2) - tq.read() + val tsem = TSemaphore.new(5) + atomically { + tsem.release() } //sampleEnd - println("Result $result") - println("Items in queue ${atomically { tq.flush() }}") + println("Permits remaining ${atomically { tsem.available() }}") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-30.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-30.kt index e74d4ab65db..bcaa4652c93 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-30.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-30.kt @@ -1,16 +1,15 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm30 -import arrow.fx.stm.TQueue +import arrow.fx.stm.TSemaphore import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tq = TQueue.new() - val result = atomically { - tq.tryRead() + val tsem = TSemaphore.new(5) + atomically { + tsem.release(2) } //sampleEnd - println("Result $result") - println("Items in queue ${atomically { tq.flush() }}") + println("Permits remaining ${atomically { tsem.available() }}") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-31.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-31.kt index 194410a648a..50df03279b4 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-31.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-31.kt @@ -7,13 +7,9 @@ import arrow.fx.stm.atomically suspend fun main() { //sampleStart val tq = TQueue.new() - val result = atomically { + atomically { tq.write(2) - tq.write(4) - - tq.flush() } //sampleEnd - println("Result $result") println("Items in queue ${atomically { tq.flush() }}") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-32.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-32.kt index f3c451dfac0..470c909380a 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-32.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-32.kt @@ -7,12 +7,9 @@ import arrow.fx.stm.atomically suspend fun main() { //sampleStart val tq = TQueue.new() - val result = atomically { - tq.write(2) - - tq.peek() + atomically { + tq += 2 } //sampleEnd - println("Result $result") println("Items in queue ${atomically { tq.flush() }}") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-33.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-33.kt index 0e1b22551d7..7e26f72dcc8 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-33.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-33.kt @@ -8,7 +8,8 @@ suspend fun main() { //sampleStart val tq = TQueue.new() val result = atomically { - tq.tryPeek() + tq.write(2) + tq.read() } //sampleEnd println("Result $result") diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-34.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-34.kt index df45719611d..2c04e69cc06 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-34.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-34.kt @@ -7,10 +7,10 @@ import arrow.fx.stm.atomically suspend fun main() { //sampleStart val tq = TQueue.new() - atomically { - tq.write(1) - tq.writeFront(2) + val result = atomically { + tq.tryRead() } //sampleEnd + println("Result $result") println("Items in queue ${atomically { tq.flush() }}") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-35.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-35.kt index 1cc776599fd..0ee70f6afe3 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-35.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-35.kt @@ -8,8 +8,12 @@ suspend fun main() { //sampleStart val tq = TQueue.new() val result = atomically { - tq.isEmpty() + tq.write(2) + tq.write(4) + + tq.flush() } //sampleEnd println("Result $result") + println("Items in queue ${atomically { tq.flush() }}") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-36.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-36.kt index faf09dbfd55..54a23363d99 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-36.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-36.kt @@ -8,8 +8,11 @@ suspend fun main() { //sampleStart val tq = TQueue.new() val result = atomically { - tq.isNotEmpty() + tq.write(2) + + tq.peek() } //sampleEnd println("Result $result") + println("Items in queue ${atomically { tq.flush() }}") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-37.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-37.kt index b3ab1c535a7..e626dc3dea4 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-37.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-37.kt @@ -7,10 +7,10 @@ import arrow.fx.stm.atomically suspend fun main() { //sampleStart val tq = TQueue.new() - atomically { - tq.write(0) - tq.removeAll { it != 0 } + val result = atomically { + tq.tryPeek() } //sampleEnd + println("Result $result") println("Items in queue ${atomically { tq.flush() }}") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-38.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-38.kt index 1882020b343..086446b6309 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-38.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-38.kt @@ -7,9 +7,10 @@ import arrow.fx.stm.atomically suspend fun main() { //sampleStart val tq = TQueue.new() - val result = atomically { - tq.size() + atomically { + tq.write(1) + tq.writeFront(2) } //sampleEnd - println("Result $result") + println("Items in queue ${atomically { tq.flush() }}") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-39.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-39.kt index 944e07063f9..9a6a75b8006 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-39.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-39.kt @@ -1,14 +1,14 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm39 -import arrow.fx.stm.TArray +import arrow.fx.stm.TQueue import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tarr = TArray.new(size = 10, 2) + val tq = TQueue.new() val result = atomically { - tarr[5] + tq.isEmpty() } //sampleEnd println("Result $result") diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-40.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-40.kt index 62e36840f94..ce19a9dc83d 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-40.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-40.kt @@ -1,16 +1,14 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm40 -import arrow.fx.stm.TArray +import arrow.fx.stm.TQueue import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tarr = TArray.new(size = 10, 2) + val tq = TQueue.new() val result = atomically { - tarr[5] = 3 - - tarr[5] + tq.isNotEmpty() } //sampleEnd println("Result $result") diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-41.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-41.kt index 6402fe8de66..1e878b95851 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-41.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-41.kt @@ -1,15 +1,16 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm41 -import arrow.fx.stm.TArray +import arrow.fx.stm.TQueue import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tarr = TArray.new(size = 10, 2) - val result = atomically { - tarr.transform { it + 1 } + val tq = TQueue.new() + atomically { + tq.write(0) + tq.removeAll { it != 0 } } //sampleEnd - println("Result $result") + println("Items in queue ${atomically { tq.flush() }}") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-42.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-42.kt index 694be8634bb..ff34e8adb30 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-42.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-42.kt @@ -1,14 +1,14 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm42 -import arrow.fx.stm.TArray +import arrow.fx.stm.TQueue import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tarr = TArray.new(size = 10, 2) + val tq = TQueue.new() val result = atomically { - tarr.fold(0) { acc, v -> acc + v } + tq.size() } //sampleEnd println("Result $result") diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-43.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-43.kt index 51c78a9d5d4..d5f309766f6 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-43.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-43.kt @@ -1,16 +1,15 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm43 -import arrow.fx.stm.TMap +import arrow.fx.stm.TArray import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tmap = TMap.new() - atomically { - tmap[1] = "Hello" - - tmap.remove(1) + val tarr = TArray.new(size = 10, 2) + val result = atomically { + tarr[5] } //sampleEnd + println("Result $result") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-44.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-44.kt index 53ff9b96509..5018b020c3a 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-44.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-44.kt @@ -1,17 +1,16 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm44 -import arrow.fx.stm.TMap +import arrow.fx.stm.TArray import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tmap = TMap.new() + val tarr = TArray.new(size = 10, 2) val result = atomically { - tmap[1] = "Hello" - tmap[2] = "World" + tarr[5] = 3 - tmap.lookup(1) + tarr[5] } //sampleEnd println("Result $result") diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-45.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-45.kt index 5918b9516cf..becba21e5b8 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-45.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-45.kt @@ -1,17 +1,14 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm45 -import arrow.fx.stm.TMap +import arrow.fx.stm.TArray import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tmap = TMap.new() + val tarr = TArray.new(size = 10, 2) val result = atomically { - tmap[1] = "Hello" - tmap[2] = "World" - - tmap[2] + tarr.transform { it + 1 } } //sampleEnd println("Result $result") diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-46.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-46.kt index a0ea6fdbd01..7135f86a595 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-46.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-46.kt @@ -1,14 +1,15 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm46 -import arrow.fx.stm.TMap +import arrow.fx.stm.TArray import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tmap = TMap.new() - atomically { - tmap.insert(10, "Hello") + val tarr = TArray.new(size = 10, 2) + val result = atomically { + tarr.fold(0) { acc, v -> acc + v } } //sampleEnd + println("Result $result") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-47.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-47.kt index 60bf1c0ba79..cf55b7e1c2a 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-47.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-47.kt @@ -9,6 +9,8 @@ suspend fun main() { val tmap = TMap.new() atomically { tmap[1] = "Hello" + + tmap.remove(1) } //sampleEnd } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-48.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-48.kt index 0d16fb19582..ca49193f6cd 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-48.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-48.kt @@ -7,8 +7,12 @@ import arrow.fx.stm.atomically suspend fun main() { //sampleStart val tmap = TMap.new() - atomically { - tmap += (1 to "Hello") + val result = atomically { + tmap[1] = "Hello" + tmap[2] = "World" + + tmap.lookup(1) } //sampleEnd + println("Result $result") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-49.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-49.kt index 40c5dc87bc4..aa2c626001e 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-49.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-49.kt @@ -8,8 +8,9 @@ suspend fun main() { //sampleStart val tmap = TMap.new() val result = atomically { - tmap[2] = "Hello" - tmap.update(2) { it.reversed() } + tmap[1] = "Hello" + tmap[2] = "World" + tmap[2] } //sampleEnd diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-50.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-50.kt index 1cf1830b2ea..26ad5f7ed03 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-50.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-50.kt @@ -8,8 +8,7 @@ suspend fun main() { //sampleStart val tmap = TMap.new() atomically { - tmap[1] = "Hello" - tmap.remove(1) + tmap.insert(10, "Hello") } //sampleEnd } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-51.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-51.kt index b3341eca789..fbe2a8294c4 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-51.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-51.kt @@ -1,16 +1,14 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm51 -import arrow.fx.stm.TSet +import arrow.fx.stm.TMap import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tset = TSet.new() - val result = atomically { - tset.insert("Hello") - tset.member("Hello") + val tmap = TMap.new() + atomically { + tmap[1] = "Hello" } //sampleEnd - println("Result $result") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-52.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-52.kt index 061e2e02d0e..134f34f4128 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-52.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-52.kt @@ -1,14 +1,14 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm52 -import arrow.fx.stm.TSet +import arrow.fx.stm.TMap import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tset = TSet.new() + val tmap = TMap.new() atomically { - tset.insert("Hello") + tmap += (1 to "Hello") } //sampleEnd } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-53.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-53.kt index a685ae6ce0e..2384af6979e 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-53.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-53.kt @@ -1,14 +1,17 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm53 -import arrow.fx.stm.TSet +import arrow.fx.stm.TMap import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tset = TSet.new() - atomically { - tset += "Hello" + val tmap = TMap.new() + val result = atomically { + tmap[2] = "Hello" + tmap.update(2) { it.reversed() } + tmap[2] } //sampleEnd + println("Result $result") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-54.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-54.kt index c8c38bca52f..083c6aba8d6 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-54.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-54.kt @@ -1,15 +1,15 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm54 -import arrow.fx.stm.TSet +import arrow.fx.stm.TMap import arrow.fx.stm.atomically suspend fun main() { //sampleStart - val tset = TSet.new() + val tmap = TMap.new() atomically { - tset.insert("Hello") - tset.remove("Hello") + tmap[1] = "Hello" + tmap.remove(1) } //sampleEnd } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-55.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-55.kt index 80e07a96211..67106db651b 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-55.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-55.kt @@ -1,17 +1,15 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm55 +import arrow.fx.stm.TSet import arrow.fx.stm.atomically -import arrow.fx.stm.stm suspend fun main() { //sampleStart - val i = 4 + val tset = TSet.new() val result = atomically { - stm { - if (i == 4) retry() - "Not 4" - } orElse { "4" } + tset.insert("Hello") + tset.member("Hello") } //sampleEnd println("Result $result") diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-56.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-56.kt index f853d293d09..4ceebbf9ce3 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-56.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-56.kt @@ -1,18 +1,14 @@ // This file was automatically generated from STM.kt by Knit tool. Do not edit. package arrow.fx.stm.examples.exampleStm56 +import arrow.fx.stm.TSet import arrow.fx.stm.atomically -import arrow.fx.stm.stm suspend fun main() { //sampleStart - val i = 4 - val result = atomically { - stm { - check(i <= 5) // This calls retry and aborts if i <= 5 - "Larger than 5" - } orElse { "Smaller than or equal to 5" } + val tset = TSet.new() + atomically { + tset.insert("Hello") } //sampleEnd - println("Result $result") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-57.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-57.kt new file mode 100644 index 00000000000..8c45075b060 --- /dev/null +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-57.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from STM.kt by Knit tool. Do not edit. +package arrow.fx.stm.examples.exampleStm57 + +import arrow.fx.stm.TSet +import arrow.fx.stm.atomically + +suspend fun main() { + //sampleStart + val tset = TSet.new() + atomically { + tset += "Hello" + } + //sampleEnd +} diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-58.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-58.kt new file mode 100644 index 00000000000..76897545e38 --- /dev/null +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-58.kt @@ -0,0 +1,15 @@ +// This file was automatically generated from STM.kt by Knit tool. Do not edit. +package arrow.fx.stm.examples.exampleStm58 + +import arrow.fx.stm.TSet +import arrow.fx.stm.atomically + +suspend fun main() { + //sampleStart + val tset = TSet.new() + atomically { + tset.insert("Hello") + tset.remove("Hello") + } + //sampleEnd +} diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-59.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-59.kt new file mode 100644 index 00000000000..4b66e7ff075 --- /dev/null +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-59.kt @@ -0,0 +1,18 @@ +// This file was automatically generated from STM.kt by Knit tool. Do not edit. +package arrow.fx.stm.examples.exampleStm59 + +import arrow.fx.stm.atomically +import arrow.fx.stm.stm + +suspend fun main() { + //sampleStart + val i = 4 + val result = atomically { + stm { + if (i == 4) retry() + "Not 4" + } orElse { "4" } + } + //sampleEnd + println("Result $result") +} diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-60.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-60.kt new file mode 100644 index 00000000000..0d16005c120 --- /dev/null +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-60.kt @@ -0,0 +1,18 @@ +// This file was automatically generated from STM.kt by Knit tool. Do not edit. +package arrow.fx.stm.examples.exampleStm60 + +import arrow.fx.stm.atomically +import arrow.fx.stm.stm + +suspend fun main() { + //sampleStart + val i = 4 + val result = atomically { + stm { + check(i <= 5) // This calls retry and aborts if i <= 5 + "Larger than 5" + } orElse { "Smaller than or equal to 5" } + } + //sampleEnd + println("Result $result") +}