cats-stm

An STM implementation for Cats Effect

License

License

GroupId

GroupId

io.github.timwspence
ArtifactId

ArtifactId

cats-stm_2.11
Last Version

Last Version

0.7.0
Release Date

Release Date

Type

Type

jar
Description

Description

cats-stm
An STM implementation for Cats Effect
Project URL

Project URL

https://timwspence.github.io/cats-stm
Project Organization

Project Organization

timwspence
Source Code Management

Source Code Management

https://github.com/TimWSpence/cats-stm

Download cats-stm_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.timwspence/cats-stm_2.11/ -->
<dependency>
    <groupId>io.github.timwspence</groupId>
    <artifactId>cats-stm_2.11</artifactId>
    <version>0.7.0</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.timwspence/cats-stm_2.11/
implementation 'io.github.timwspence:cats-stm_2.11:0.7.0'
// https://jarcasting.com/artifacts/io.github.timwspence/cats-stm_2.11/
implementation ("io.github.timwspence:cats-stm_2.11:0.7.0")
'io.github.timwspence:cats-stm_2.11:jar:0.7.0'
<dependency org="io.github.timwspence" name="cats-stm_2.11" rev="0.7.0">
  <artifact name="cats-stm_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.timwspence', module='cats-stm_2.11', version='0.7.0')
)
libraryDependencies += "io.github.timwspence" % "cats-stm_2.11" % "0.7.0"
[io.github.timwspence/cats-stm_2.11 "0.7.0"]

Dependencies

compile (4)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.12
org.typelevel : cats-effect_2.11 jar 2.0.0
org.typelevel : cats-core_2.11 jar 2.0.0
org.scala-lang.modules : scala-java8-compat_2.11 jar 0.9.0

test (6)

Group / Artifact Type Version
com.github.alexarchambault : scalacheck-shapeless_1.14_2.11 jar 1.2.4
org.typelevel : cats-laws_2.11 jar 2.0.0
org.typelevel : discipline-scalatest_2.11 jar 1.0.0
org.scalatest : scalatest_2.11 jar 3.1.0-RC2
org.scalacheck : scalacheck_2.11 jar 1.14.3
org.scalatestplus : scalatestplus-scalacheck_2.11 jar 3.1.0.0-RC2

Project Modules

There are no modules declared in this project.

Cats STM

Build Status Join the chat at https://gitter.im/cats-stm/community Scala Steward badge

An implementation of Software Transactional Memory for Cats Effect, inspired by Beautiful Concurrency.

For more information, see the documentation.

Usage

libraryDependencies += "io.github.timwspence" %% "cats-stm" % "0.8.0"

The core abstraction is the TVar (transactional var), which exposes operations in the Txn monad. Once constructed, Txn actions can be atomically evaluated in the IO monad.

Here is a contrived example of what this looks like in practice. We use the check combinator to retry transferring money from Tim and Steve until we have enough money in Tim's account:

import scala.concurrent.duration._

import cats.effect.unsafe.implicits.global
import cats.effect.{IO, IOApp}

object Main extends IOApp.Simple {

  val stm = STM.runtime[IO].unsafeRunSync()
  import stm._

  override def run: IO[Unit] =
    for {
      accountForTim   <- stm.commit(TVar.of[Long](100))
      accountForSteve <- stm.commit(TVar.of[Long](0))
      _               <- printBalances(accountForTim, accountForSteve)
      _               <- giveTimMoreMoney(accountForTim).start
      _               <- transfer(accountForTim, accountForSteve)
      _               <- printBalances(accountForTim, accountForSteve)
    } yield ()

  private def transfer(accountForTim: TVar[Long], accountForSteve: TVar[Long]): IO[Unit] =
    stm.commit {
      for {
        balance <- accountForTim.get
        _       <- stm.check(balance > 100)
        _       <- accountForTim.modify(_ - 100)
        _       <- accountForSteve.modify(_ + 100)
      } yield ()
    }

  private def giveTimMoreMoney(accountForTim: TVar[Long]): IO[Unit] =
    for {
      _ <- IO.sleep(5000.millis)
      _ <- stm.commit(accountForTim.modify(_ + 1))
    } yield ()

  private def printBalances(accountForTim: TVar[Long], accountForSteve: TVar[Long]): IO[Unit] =
    for {
      (amountForTim, amountForSteve) <- stm.commit(for {
        t <- accountForTim.get
        s <- accountForSteve.get
      } yield (t, s))
      _ <- IO(println(s"Tim: $amountForTim"))
      _ <- IO(println(s"Steve: $amountForSteve"))
    } yield ()

}

Documentation

The documentation is built using docusaurus. You can generate it via nix-shell --run "sbt docs/docusaurusCreateSite" . You can then view it via nix-shell --run "cd website && npm start".

Credits

This software was inspired by Beautiful Concurrency and the stm package.

Many thanks to @impurepics for the awesome logo!

Tool Sponsorship

Development of Cats STM is generously supported in part by YourKit through the use of their excellent Java profiler.

Versions

Version
0.7.0
0.6.0
0.5.0
0.4.0
0.3.0