stm4cats


License

License

MIT
GroupId

GroupId

com.olegpy
ArtifactId

ArtifactId

stm4cats_2.11
Last Version

Last Version

0.1.0-M1
Release Date

Release Date

Type

Type

jar
Description

Description

stm4cats
stm4cats
Project URL

Project URL

https://github.com/oleg-py/stm4cats
Project Organization

Project Organization

com.olegpy
Source Code Management

Source Code Management

https://github.com/oleg-py/stm4cats

Download stm4cats_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/com.olegpy/stm4cats_2.11/ -->
<dependency>
    <groupId>com.olegpy</groupId>
    <artifactId>stm4cats_2.11</artifactId>
    <version>0.1.0-M1</version>
</dependency>
// https://jarcasting.com/artifacts/com.olegpy/stm4cats_2.11/
implementation 'com.olegpy:stm4cats_2.11:0.1.0-M1'
// https://jarcasting.com/artifacts/com.olegpy/stm4cats_2.11/
implementation ("com.olegpy:stm4cats_2.11:0.1.0-M1")
'com.olegpy:stm4cats_2.11:jar:0.1.0-M1'
<dependency org="com.olegpy" name="stm4cats_2.11" rev="0.1.0-M1">
  <artifact name="stm4cats_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.olegpy', module='stm4cats_2.11', version='0.1.0-M1')
)
libraryDependencies += "com.olegpy" % "stm4cats_2.11" % "0.1.0-M1"
[com.olegpy/stm4cats_2.11 "0.1.0-M1"]

Dependencies

compile (2)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.12
org.typelevel : cats-effect_2.11 jar 1.2.0

test (3)

Group / Artifact Type Version
com.lihaoyi : utest_2.11 jar 0.6.7
org.typelevel : cats-laws_2.11 jar 1.5.0
org.typelevel : cats-effect-laws_2.11 jar 1.2.0

Project Modules

There are no modules declared in this project.

stm4cats

Maven Central Build Status Coverage Status

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:

// 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.

resolvers += Resolver.sonatypeRepo("snapshots")
libraryDependencies += "com.olegpy" %% "stm4cats" % "0.1.0-SNAPSHOT"

Try it

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:

Versions

Version
0.1.0-M1