Skip to content

Instantly share code, notes, and snippets.

@tpolecat
Last active December 17, 2015 21:08
Show Gist options
  • Save tpolecat/5672105 to your computer and use it in GitHub Desktop.
Save tpolecat/5672105 to your computer and use it in GitHub Desktop.
making scala stm look like haskell stm
package util
import scalaz.effect.IO
import scalaz._
import Scalaz._
import scala.concurrent.stm.{ retry => stmRetry, _ }
object X {
type STM[+A] = ReaderT[IO, InTxn, A]
type TVar[A] = Ref[A]
def readTVar[A](r: TVar[A]): STM[A] =
Reader { implicit t: InTxn => r() }.lift[IO]
def putTVar[A](r: TVar[A], a: A): STM[Unit] =
Reader { implicit t: InTxn => r() = a }.lift[IO]
val retry: STM[Nothing] =
Reader { implicit t: InTxn => stmRetry }.lift[IO]
def newTVar[A](a: A): IO[TVar[A]] =
IO(Ref(a))
def atomically[A](a: STM[A]): IO[A] =
atomic(a.run)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment