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 aa5ca900e0c..895be245622 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 @@ -55,7 +55,7 @@ import arrow.fx.stm.internal.lookupHamtWithHash * * fun STM.withdraw(acc: TVar, amount: Int): Unit { * val current = acc.read() - * if (current - amount >= 0) acc.write(current + amount) + * if (current - amount >= 0) acc.write(current - amount) * else throw IllegalStateException("Not enough money in the account!") * } * //sampleEnd @@ -112,7 +112,7 @@ import arrow.fx.stm.internal.lookupHamtWithHash * * fun STM.withdraw(acc: TVar, amount: Int): Unit { * val current = acc.read() - * if (current - amount >= 0) acc.write(current + amount) + * if (current - amount >= 0) acc.write(current - amount) * else retry() // we now retry if there is not enough money in the account * // this can also be achieved by using `check(current - amount >= 0); acc.write(it + amount)` * } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-01.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-01.kt index c74156a49ce..c48332f9f87 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-01.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-01.kt @@ -18,7 +18,7 @@ fun STM.deposit(acc: TVar, amount: Int): Unit { fun STM.withdraw(acc: TVar, amount: Int): Unit { val current = acc.read() - if (current - amount >= 0) acc.write(current + amount) + if (current - amount >= 0) acc.write(current - amount) else throw IllegalStateException("Not enough money in the account!") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-02.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-02.kt index bdeaf829b0b..af8152800bb 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-02.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-02.kt @@ -21,7 +21,7 @@ fun STM.deposit(acc: TVar, amount: Int): Unit { fun STM.withdraw(acc: TVar, amount: Int): Unit { val current = acc.read() - if (current - amount >= 0) acc.write(current + amount) + if (current - amount >= 0) acc.write(current - amount) else retry() // we now retry if there is not enough money in the account // this can also be achieved by using `check(current - amount >= 0); acc.write(it + amount)` }