All Projects → TimWSpence → Cats Stm

TimWSpence / Cats Stm

Licence: apache-2.0
An STM implementation for Cats Effect

Programming Languages

scala
5932 projects

Projects that are alternatives of or similar to Cats Stm

Cats Mtl
cats transformer type classes.
Stars: ✭ 238 (+124.53%)
Mutual labels:  monad, cats, functional-programming
Static Land
Specification for common algebraic structures in JavaScript based on Fantasy Land
Stars: ✭ 699 (+559.43%)
Mutual labels:  monad, functional-programming
Learn Fp
learn-by-doing course/tutorial for functional programming on scala
Stars: ✭ 548 (+416.98%)
Mutual labels:  monad, functional-programming
Es Cqrs Shopping Cart
A resilient and scalable shopping cart system designed using Event Sourcing (ES) and Command Query Responsibility Segregation (CQRS)
Stars: ✭ 19 (-82.08%)
Mutual labels:  cats, functional-programming
Kotlin Result
A multiplatform Result monad for modelling success or failure operations.
Stars: ✭ 369 (+248.11%)
Mutual labels:  monad, functional-programming
Dry Monads
Useful, common monads in idiomatic Ruby
Stars: ✭ 453 (+327.36%)
Mutual labels:  monad, functional-programming
Fp Core.rs
A library for functional programming in Rust
Stars: ✭ 772 (+628.3%)
Mutual labels:  monad, functional-programming
Scriptum
A fool's scriptum on functional programming
Stars: ✭ 346 (+226.42%)
Mutual labels:  monad, functional-programming
Stm4cats
STM monad for cats-effect
Stars: ✭ 35 (-66.98%)
Mutual labels:  cats, functional-programming
Purefun
Functional Programming library for Java
Stars: ✭ 37 (-65.09%)
Mutual labels:  monad, functional-programming
Tsoption
Correct, easy to use Option type for TypeScript. 🦄
Stars: ✭ 53 (-50%)
Mutual labels:  monad, functional-programming
Fp Resources
Functional programming great resources
Stars: ✭ 369 (+248.11%)
Mutual labels:  monad, functional-programming
Fun Task
Abstraction for managing asynchronous code in JS
Stars: ✭ 363 (+242.45%)
Mutual labels:  monad, functional-programming
Bow
🏹 Bow is a cross-platform library for Typed Functional Programming in Swift
Stars: ✭ 538 (+407.55%)
Mutual labels:  monad, functional-programming
Language Ext
C# functional language extensions - a base class library for functional programming
Stars: ✭ 3,964 (+3639.62%)
Mutual labels:  monad, functional-programming
Lambda
Functional patterns for Java
Stars: ✭ 737 (+595.28%)
Mutual labels:  monad, functional-programming
Cyclops
An advanced, but easy to use, platform for writing functional applications in Java 8.
Stars: ✭ 1,180 (+1013.21%)
Mutual labels:  monad, functional-programming
Monio
Async-capable IO monad for JS
Stars: ✭ 311 (+193.4%)
Mutual labels:  monad, functional-programming
Magic In Ten Mins
十分钟魔法练习
Stars: ✭ 327 (+208.49%)
Mutual labels:  monad, functional-programming
Purify
Functional programming library for TypeScript - https://gigobyte.github.io/purify/
Stars: ✭ 843 (+695.28%)
Mutual labels:  monad, functional-programming

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].