Embed presentation
Download to read offline


(f: A => B): List[B]](https://crownmelresort.com/image.slidesharecdn.com/option-160505093726/75/Funcional-Programming-in-Scala-Chapter-4-3-2048.jpg)
![CHAPTER 4
EXAMPLE
// partial function
def mean(xs: Seq[Double]): Double =
if (xs.isEmpty)
throw new ArithmeticException("mean of
empty list!")
else xs.sum / xs.length
def mean1(xs: Seq[Double]): Double =
if (xs.isEmpty)
null
else xs.sum / xs.length](https://crownmelresort.com/image.slidesharecdn.com/option-160505093726/75/Funcional-Programming-in-Scala-Chapter-4-4-2048.jpg)
![CHAPTER 4
EXAMPLE
// total function
def mean_1(xs: Seq[Double], onEmpty: Double):
Double =
if (xs.isEmpty)
onEmpty
else
xs.sum / xs.length](https://crownmelresort.com/image.slidesharecdn.com/option-160505093726/75/Funcional-Programming-in-Scala-Chapter-4-5-2048.jpg)
![CHAPTER 4
OPTION
sealed trait Option[+A]
case class Some[+A](get: A) extends Option[A]
case object None extends Option[Nothing]](https://crownmelresort.com/image.slidesharecdn.com/option-160505093726/75/Funcional-Programming-in-Scala-Chapter-4-6-2048.jpg)


![CHAPTER 4
MONAD
trait M[A] {
def flatMap[B](f: A => M[B]): M[B]
}
def unit[A](x: A): M[A]](https://crownmelresort.com/image.slidesharecdn.com/option-160505093726/75/Funcional-Programming-in-Scala-Chapter-4-9-2048.jpg)


The document discusses handling errors in a functional programming style without exceptions. It covers the main problems with exceptions, including context dependence and lack of type safety. It then presents the Option type as an alternative, which is sealed and can be either Some containing a value or None. The document explains how to use the Option type correctly and introduces concepts like map, flatMap, and monads for structural recursion with the Option type.


(f: A => B): List[B]](https://crownmelresort.com/image.slidesharecdn.com/option-160505093726/75/Funcional-Programming-in-Scala-Chapter-4-3-2048.jpg)
![CHAPTER 4
EXAMPLE
// partial function
def mean(xs: Seq[Double]): Double =
if (xs.isEmpty)
throw new ArithmeticException("mean of
empty list!")
else xs.sum / xs.length
def mean1(xs: Seq[Double]): Double =
if (xs.isEmpty)
null
else xs.sum / xs.length](https://crownmelresort.com/image.slidesharecdn.com/option-160505093726/75/Funcional-Programming-in-Scala-Chapter-4-4-2048.jpg)
![CHAPTER 4
EXAMPLE
// total function
def mean_1(xs: Seq[Double], onEmpty: Double):
Double =
if (xs.isEmpty)
onEmpty
else
xs.sum / xs.length](https://crownmelresort.com/image.slidesharecdn.com/option-160505093726/75/Funcional-Programming-in-Scala-Chapter-4-5-2048.jpg)
![CHAPTER 4
OPTION
sealed trait Option[+A]
case class Some[+A](get: A) extends Option[A]
case object None extends Option[Nothing]](https://crownmelresort.com/image.slidesharecdn.com/option-160505093726/75/Funcional-Programming-in-Scala-Chapter-4-6-2048.jpg)


![CHAPTER 4
MONAD
trait M[A] {
def flatMap[B](f: A => M[B]): M[B]
}
def unit[A](x: A): M[A]](https://crownmelresort.com/image.slidesharecdn.com/option-160505093726/75/Funcional-Programming-in-Scala-Chapter-4-9-2048.jpg)
