Skip to content

Commit

Permalink
Step 10: Generic F-program
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaparadela committed Nov 26, 2018
1 parent df0ede5 commit bf542ad
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/main/scala/training/Program.scala
Original file line number Diff line number Diff line change
@@ -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)

}

0 comments on commit bf542ad

Please sign in to comment.