All Projects → oleg-py → Stm4cats

oleg-py / Stm4cats

Licence: mit
STM monad for cats-effect

Programming Languages

scala
5932 projects

Projects that are alternatives of or similar to Stm4cats

Sup
Composable, purely functional healthchecks in Scala.
Stars: ✭ 138 (+294.29%)
Mutual labels:  cats, functional-programming, fp
Test State
Scala Test-State.
Stars: ✭ 119 (+240%)
Mutual labels:  cats, functional-programming, fp
Pfps Shopping Cart
🛒 The Shopping Cart application developed in the book "Practical FP in Scala: A hands-on approach"
Stars: ✭ 262 (+648.57%)
Mutual labels:  cats, functional-programming, fp
Monix
Asynchronous, Reactive Programming for Scala and Scala.js.
Stars: ✭ 1,819 (+5097.14%)
Mutual labels:  cats, functional-programming, fp
Monio
Async-capable IO monad for JS
Stars: ✭ 311 (+788.57%)
Mutual labels:  functional-programming, fp
Prelude Ts
Functional programming, immutable collections and FP constructs for typescript and javascript
Stars: ✭ 315 (+800%)
Mutual labels:  functional-programming, fp
Kotlin Result
A multiplatform Result monad for modelling success or failure operations.
Stars: ✭ 369 (+954.29%)
Mutual labels:  functional-programming, fp
Fp Jargon Zh
函数式编程术语及示例。本项目译自 https://github.com/hemanth/functional-programming-jargon
Stars: ✭ 507 (+1348.57%)
Mutual labels:  functional-programming, fp
tradeio
A disciplined way to purely functional domain models in Scala
Stars: ✭ 19 (-45.71%)
Mutual labels:  cats, fp
Cats Infographic
typeclass diagram for cats
Stars: ✭ 403 (+1051.43%)
Mutual labels:  cats, fp
Flawless
WIP Delightful, purely functional testing no-framework. Don't even try to use it at work!
Stars: ✭ 33 (-5.71%)
Mutual labels:  functional-programming, fp
Shapeless
Generic programming for Scala
Stars: ✭ 3,207 (+9062.86%)
Mutual labels:  functional-programming, fp
Eslint Plugin Functional
ESLint rules to disable mutation and promote fp in JavaScript and TypeScript.
Stars: ✭ 282 (+705.71%)
Mutual labels:  functional-programming, fp
Fun Task
Abstraction for managing asynchronous code in JS
Stars: ✭ 363 (+937.14%)
Mutual labels:  functional-programming, fp
Funfix
Functional Programming Library for JavaScript, TypeScript and Flow ✨⚡️
Stars: ✭ 596 (+1602.86%)
Mutual labels:  functional-programming, fp
Fasy
FP iterators that are both eager and asynchronous
Stars: ✭ 488 (+1294.29%)
Mutual labels:  functional-programming, fp
Fkit
A functional programming toolkit for JavaScript.
Stars: ✭ 588 (+1580%)
Mutual labels:  functional-programming, fp
Frameless
Expressive types for Spark.
Stars: ✭ 717 (+1948.57%)
Mutual labels:  functional-programming, fp
D4s
Dynamo DB Database Done Scala-way
Stars: ✭ 27 (-22.86%)
Mutual labels:  functional-programming, fp
scala-functional-programming-tutorial
Functional Programming in Scala Tutorial
Stars: ✭ 23 (-34.29%)
Mutual labels:  cats, fp

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:

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