Skip to content

Instantly share code, notes, and snippets.

@trepidacious
Created July 6, 2018 14:58
Show Gist options
  • Save trepidacious/348975e79a9ea97b01d053a8b7dd27ed to your computer and use it in GitHub Desktop.
Save trepidacious/348975e79a9ea97b01d053a8b7dd27ed to your computer and use it in GitHub Desktop.
/**
* A Transaction can produce an effect and a result using STMOps. This represents
* an atomic operation performed using the STMOps, getting/setting data, etc.
* Using a wrapper allows for use of different effects, and serialisation.
*/
trait Transaction[A] {
def apply[F[_]: Monad](implicit stm: STMOps[F]): F[A]
def map[B](f: A => B): Transaction[B] = {
val t = this
new Transaction[B] {
override def apply[F[_] : Monad](implicit stm: STMOps[F]): F[B] = t[F].map(f)
}
}
def flatMap[B](f: A => Transaction[B]): Transaction[B] = {
val t = this
new Transaction[B] {
override def apply[F[_] : Monad](implicit stm: STMOps[F]): F[B] = t[F].flatMap(a => f(a)[F])
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment