-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
rafaparadela
committed
Nov 26, 2018
1 parent
df0ede5
commit bf542ad
Showing
1 changed file
with
21 additions
and
8 deletions.
There are no files selected for viewing
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
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) | ||
|
||
} | ||
|