Last active
December 17, 2015 21:08
-
-
Save tpolecat/5672105 to your computer and use it in GitHub Desktop.
making scala stm look like haskell stm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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