3. def withdrawMoney(user: String,
password: String, amount: Int): Money = {
// some logic
Money(amount)
}
def buyBook(name: String, cash: Money): Book = {
// some logic
Book("Functional Programming in Scala")
}
val cash = withdrawMoney("user1", "password2", 100)
val book = buyBook("Functional Programming in Scala", cash)
println(book)
4. trait Try[+T]
case class Success[+T](value: T) extends Try[T]
case class Failure(e: Throwable) extends Try[Nothing]
val success = Success(100)
success match {
case Success(v) => println(v)
case Failure(e) => println(s"Error: $e")
}
5. val v = Try {
if (scala.util.Random.nextBoolean() == true) 12
else 1 / 0
}
v match {
case Success(v) => println(v)
case Failure(e) => println("Error!")
}
6. def withdrawMoney(user: String,
password: String, amount: Int): Try[Money] = {
Try(Money(amount))
}
def buyBook(name: String, cash: Money): Try[Book] = {
Try(Book("Functional Programming in Scala"))
}
withdrawMoney("user1", "password2", 100) match {
case Success(cash) => {
buyBook("Functional Programming in Scala", cash) match {
case Success(book) => println(book)
case Failure(e) => println(s"Error occurred: $e")
}
}
case Failure(e) => println(s"Error occurred: $e")
}
7. case class Money(amount: Int)
case class Book(title: String)
def withdrawMoney(user: String, password:
String, amount: Int): Try[Money] = {
// some logic
Try(Money(amount))
}
def buyBook(name: String, cash: Money): Try[Book] = {
// some logic
Try(Book("Functional Programming in Scala"))
}
for {
cash <- withdrawMoney("user1", "password2", 100)
book <- buyBook("Functional Programming in Scala", cash)
} println(book)
8. case class Money(amount: Int)
case class Book(title: String)
def withdrawMoney(user: String, password:
String, amount: Int): Try[Money] = {
// some logic
Try(Money(amount))
}
def buyBook(name: String, cash: Money): Try[Book] = {
// some logic
Try(Book("Functional Programming in Scala"))
}
val book = withdrawMoney("user1", "password2", 100)
.flatMap(cash => buyBook("Functional Programming in Scala", cash))
book.foreach(b => println(b))
}
9. trait Option[+A]
case class Some[+A](v: A) extends Option[A]
case object None extends Option[Nothing]
def unit[A](v: A): Option[A] = {
Some(v)
}
def flatMap[A, B](op: Option[A])(f: A => Option[B]): Option[B]
= op match {
case Some(v) => f(v)
case None => None
}
def map[A, B](op: Option[A])(f: A => B): Option[B] = {
flatMap(op)(x => unit(f(x)))
}
10. The Four Essential Effects In Programming
One Many
Synchronous T/Try[T] Iterable[T]
Asynchronous Future[T] Observable[T]
16. val f1 = Future { 1 }
val f2 = Future { 1 / 0 }
val f3 = Future { 2 }
val c = for {
v1 <- f1
v2 <- f2
v3 <- f3
} yield v1 + v2 + v3
c onComplete {
case Success(v) => println(v)
case Failure(e) => println(s"error $e")
}
Thread.sleep(1000)
17. def getRandonName = Future { "John" }
def getIdByName(name: String) = Future { 12 }
def getDepotIdById(id: Int) = Future { 34 }
def getDepotName(id: Int) = Future { "A depot" }
val f = for {
name <- getRandonName
userId <- getIdByName(name)
depotId <- getDepotIdById(userId)
depotName <- getDepotName(userId)
} yield s"$name from $depotName"
f.onSuccess {
case (v) => println(v)
}
Thread.sleep(1000)
18. def getUSDQuote = Future { 1.2 }
def getEURQuote = Future { 0.8 }
val p = for {
usd <- getUSDQuote
eur <- getEURQuote
if (eur > usd)
} yield usd
p onFailure { case (e) => println("Error: " + e) }
Thread.sleep(1000)
19. val f1: Future[Int] = Future { 1 / 0 }
val f2: Future[Any] = f1.recover {
case (exp) => s"error happend: $exp"
}
f2.onComplete {
case Success(v) => println(s"if success: $v")
case Failure(e) => println(s"if failure: $e")
}
Thread.sleep(1000)
20. val p = Promise[String]
val q = Promise[String]
p.future foreach { case x => println(x)}
q.future.failed foreach { case e => println(s"error: " + e)}
p.complete(Success("Promise complete"))
q.complete(Failure(new Exception))
val z = Promise[Int]
z.future onComplete {
case Success(v) => println(v)
case Failure(e) => println(s"Error: $e")
}
z.complete(Try(1 / 0))
21. def first[T](xs: List[Future[T]]): Future[T] = {
val p = Promise[T]
for {
x <- xs
} p.tryCompleteWith(x)
p.future
}
val f = first(List(Future{ Thread.sleep(2000);
12 }, Future { new Exception }))
f.foreach { case x => println(x) }
22. val urlSpecSize = Future {
val url = "http://www.scala-lang.org"
scala.io.Source.fromURL(url).size
}
// how much we should wait until exception
val size = Await.result(urlSpecSize, 10 seconds)
println(size)