From bf542adf83f6443743569ece324be4f3054a0135 Mon Sep 17 00:00:00 2001 From: rafaparadela Date: Mon, 26 Nov 2018 02:18:15 +0100 Subject: [PATCH] Step 10: Generic F-program --- src/main/scala/training/Program.scala | 29 +++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/scala/training/Program.scala b/src/main/scala/training/Program.scala index 1250767..1120e69 100644 --- a/src/main/scala/training/Program.scala +++ b/src/main/scala/training/Program.scala @@ -1,30 +1,43 @@ package training +import cats.{Id, Monad} import cats.implicits._ +import scala.concurrent.Future +import scala.concurrent.ExecutionContext.Implicits.global +import scala.util.Try -object Program extends App { +class MyProgram[F[_]: Monad] extends App { case class Account(id: String, balance: Int) case class Statement(isRich: Boolean, accounts: Int) - def getBank1Credentials: Option[String] = Some("MyUser_MyPassword") + def getBank1Credentials: F[String] = "MyUser_MyPassword".pure[F] - def getBalanceBank1(credentials: String): Option[Account] = Some(Account("a1", 100)) + def getBalanceBank1(credentials: String): F[Account] = Account("a1", 100).pure[F] - def getBalanceBank2: Option[Int] = Some(80) + def getBalanceBank2: F[Int] = 80.pure[F] val moneyInPocket: Int = 20 - def balance: Option[Int] = for { + def balance: F[Int] = for { c <- getBank1Credentials b1 <- getBalanceBank1(c).map(_.balance) b2 <- getBalanceBank2 - p <- moneyInPocket.pure[Option] + p <- moneyInPocket.pure[F] } yield b1 + b2 + p +} + +object Program extends App { + + type EitherS[A] = Either[String, A] //EDGE OF THE WORLD - println(balance) + println(new MyProgram[Id].balance) + println(new MyProgram[EitherS].balance) + println(new MyProgram[Option].balance) + println(new MyProgram[Try].balance) + println(new MyProgram[List].balance) + println(new MyProgram[Future].balance) } -