KO
|
EN
gitlite — search
Search
#python
#ai
#mcp
#php
#nextjs
#react
#rust
#llm
#golang
#cli
#postgresql
#open-source
stm4cats
★ 36
Open GitHub ↗
STM monad for cats-effect
Download README (.md)
Explore Similar Repositories
ccso-nameserver
:
Resurrecting the Qi/Ph nameserver from '93
node-red-app
:
WARNING: This repository is no longer maintained
effrit
:
Go Efferent and Afferent package metric calculator.
faasbenchmark
:
Run or add generic tests to accurately benchmark FaaS providers. https://faastest.com/
webflux-mdc
:
No description available.
// repository documentation
Was this content helpful?
★ 0
(0 ratings)
Select Rating:
★
★
★
★
★
Submit Feedback
Recent Feedback
×
Download README
Do you want to download the
README.md
file for
stm4cats
?
Download (.md)
# stm4cats  [](https://travis-ci.org/oleg-py/stm4cats) [](https://coveralls.io/github/oleg-py/stm4cats?branch=master) An implementation of STM for any cats-effect compatible effect type. Current stable version is `0.1.0-M1`, available for Scala 2.11 and 2.12 and Scala.JS 0.6: ```scala // Use %%% for Scala.JS libraryDependencies += "com.olegpy" %% "stm4cats" % "0.1.0-M1" ``` Or, if you're feeling adventurous, a snapshot is build from `master` on each commit. ```scala resolvers += Resolver.sonatypeRepo("snapshots") libraryDependencies += "com.olegpy" %% "stm4cats" % "0.1.0-SNAPSHOT" ``` ### Try it ```scala import cats.implicits._ import cats.effect.IO import com.olegpy.stm._ import scala.concurrent.ExecutionContext.global import scala.concurrent.duration._ implicit val cs = IO.contextShift(global) implicit val timer = IO.timer(global) def transfer(fromR: TRef[Int], toR: TRef[Int], amt: Int): STM[Unit] = for { from <- fromR.get if from >= amt // Or STM.check(from >= amt) _ <- fromR.update(_ - amt) _ <- toR.update(_ + amt) } yield () def freeMoney(toR: TRef[Int]): IO[Unit] = STM.atomically[IO] { toR.update(_ + 10) } val io = for { fromR <- TRef(0).commit[IO] // Or shorter syntax: toR <- TRef.in[IO](0) amt = 100 f1 <- transfer(fromR, toR, amt).commit[IO].start f2 <- (freeMoney(fromR) >> IO.sleep(1.second)).foreverM.start // In 10 seconds, the transfer succeeds _ <- f1.join _ <- f2.cancel res <- toR.get.commit[IO] _ <- IO(assert(res == amt)) } yield () io.unsafeRunSync() // Well, not on JS ``` ### Acknowledgements My interest in STM, as well as some of API in stm4cats was influenced by: - [Talk](https://www.youtube.com/watch?v=d6WWmia0BPM) by @jdegoes and @wi101 on STM in ZIO - An [alternative implementation](https://github.com/TimWSpence/cats-stm) by @TimWSpence - And last, but not least, [Beautiful concurrency paper](https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/beautiful.pdf) by Simon Peyton Jones