Skip to content

Instantly share code, notes, and snippets.

@Tvaroh
Created March 1, 2019 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tvaroh/01d753cd6b1ae384e78e3e828c812d95 to your computer and use it in GitHub Desktop.
Save Tvaroh/01d753cd6b1ae384e78e3e828c812d95 to your computer and use it in GitHub Desktop.
ZIO approach with TF
import cats.~>
object playground {
// typeclasses
trait MIO[F[_], R, +E, +A] {
def map[B](f: A => B): MIO[F, R, E, B]
def flatMap[R1 <: R, E1 >: E, B](k: A => MIO[F, R1, E1, B]): MIO[F, R1, E1, B]
def transform[G[_]](nt: F ~> G): MIO[G, R, E, A]
}
trait EnvAccess[F[_]] {
def accessM[R, E, A](f: R => MIO[F, Any, E, A]): MIO[F, R, E, A]
}
object EnvAccess {
def apply[F[_]](implicit F: EnvAccess[F]): EnvAccess[F] = F
def accessM[F[_]: EnvAccess, R, E, A](f: R => MIO[F, Any, E, A]): MIO[F, R, E, A] =
EnvAccess[F].accessM(f)
}
// db
trait EvalDB[F[_], DB[_]] {
def evalDb: DB ~> F
}
// model
trait Currency
// repository
trait CurrencyRepository[F[_]] {
def currencyRepository: CurrencyRepository.Api[F]
}
object CurrencyRepository {
trait Api[F[_]] {
def findAllCurrencies: MIO[F, Any, Nothing, Seq[Currency]]
}
}
// service
trait CurrencyService[F[_], DB[_]] {
def currencyService: CurrencyService.Api[F, DB]
}
object CurrencyService {
trait Api[F[_], DB[_]] {
def findAllCurrencies: MIO[F, CurrencyRepository[DB] with EvalDB[F, DB], Nothing, Seq[Currency]]
}
class Impl[F[_]: EnvAccess, DB[_]] extends Api[F, DB] {
override def findAllCurrencies: MIO[F, CurrencyRepository[DB] with EvalDB[F, DB], Nothing, Seq[Currency]] =
EnvAccess.accessM { env =>
env.currencyRepository.findAllCurrencies.transform[F](env.evalDb)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment