SlideShare a Scribd company logo
HANDLING ERRORS WITHOUT EXCEPTIONS
FUNCTIONAL PROGRAMMING IN SCALA
SIDE EFFECT
CHAPTER 4
MAIN PROBLEMS WITH EXCEPTIONS
▸ Context dependence
▸ Exceptions are not type-safe
▸ Checked exceptions
def map[A,B](l: List[A])(f: A => B): List[B]
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
CHAPTER 4
EXAMPLE
// total function

def mean_1(xs: Seq[Double], onEmpty: Double):
Double =

if (xs.isEmpty) 

onEmpty

else 

xs.sum / xs.length
CHAPTER 4
OPTION
sealed trait Option[+A]



case class Some[+A](get: A) extends Option[A]



case object None extends Option[Nothing]
CHAPTER 4
HOW TO USE OPTION TYPE CORRECTLY
▸ Reference Option example
CHAPTER 4
STRUCTURAL RECURSION
▸ map
▸ flatMap
CHAPTER 4
MONAD
trait M[A] {
  def flatMap[B](f: A => M[B]): M[B]
}
def unit[A](x: A): M[A]
CHAPTER 4
REFERENCE
▸ Monad Design Pattern in Java
▸ Demystifying the Monad in Scala

More Related Content

What's hot (14)

PPT
StackArray stack3
Rajendran
 
PPT
Functional programming with_scala
Raymond Tay
 
DOCX
D404 ex-4-2
Omkar Rane
 
ODP
Beginning Scala Svcc 2009
David Pollak
 
PPT
Java Script Introduction
jason hu 金良胡
 
PPTX
Introduction to array and string
MuntasirMuhit
 
PPTX
DS- Stack ADT
MythiliMurugan3
 
PPTX
An Overview of the Java Programming Language
Salaam Kehinde
 
PDF
C Language Lecture 12
Shahzaib Ajmal
 
ODP
General method best_practices
parag
 
PDF
Scala laboratory: Globus. iteration #2
Vasil Remeniuk
 
PDF
05 - Scala. List type
Roman Brovko
 
PDF
Ten-page Brief Overview of Swift for Scala Developers
ihji
 
StackArray stack3
Rajendran
 
Functional programming with_scala
Raymond Tay
 
D404 ex-4-2
Omkar Rane
 
Beginning Scala Svcc 2009
David Pollak
 
Java Script Introduction
jason hu 金良胡
 
Introduction to array and string
MuntasirMuhit
 
DS- Stack ADT
MythiliMurugan3
 
An Overview of the Java Programming Language
Salaam Kehinde
 
C Language Lecture 12
Shahzaib Ajmal
 
General method best_practices
parag
 
Scala laboratory: Globus. iteration #2
Vasil Remeniuk
 
05 - Scala. List type
Roman Brovko
 
Ten-page Brief Overview of Swift for Scala Developers
ihji
 

Similar to Funcional Programming in Scala - Chapter 4 (16)

PDF
Fp in scala part 2
Hang Zhao
 
PDF
Fp in scala part 1
Hang Zhao
 
PDF
Functions, Types, Programs and Effects
Raymond Roestenburg
 
PDF
Type class survival guide
Mark Canlas
 
PDF
From OOP To FP Through A Practical Case
Cristina Delgado Rodríguez
 
PPTX
Scala for curious
Tim (dev-tim) Zadorozhniy
 
PDF
Scala Paradigms
Tom Flaherty
 
PDF
Lecture 3
Muhammad Fayyaz
 
PDF
Option - A Better Way to Handle Null Value
Jiaming Zhang
 
PDF
Practical cats
Raymond Tay
 
PDF
Scala 3 enum for a terser Option Monad Algebraic Data Type
Philip Schwarz
 
PDF
First-Class Patterns
John De Goes
 
PDF
Scala jargon cheatsheet
Ruslan Shevchenko
 
PDF
Procedure Typing for Scala
akuklev
 
PDF
Option, Either, Try and what to do with corner cases when they arise
Michal Bigos
 
Fp in scala part 2
Hang Zhao
 
Fp in scala part 1
Hang Zhao
 
Functions, Types, Programs and Effects
Raymond Roestenburg
 
Type class survival guide
Mark Canlas
 
From OOP To FP Through A Practical Case
Cristina Delgado Rodríguez
 
Scala for curious
Tim (dev-tim) Zadorozhniy
 
Scala Paradigms
Tom Flaherty
 
Lecture 3
Muhammad Fayyaz
 
Option - A Better Way to Handle Null Value
Jiaming Zhang
 
Practical cats
Raymond Tay
 
Scala 3 enum for a terser Option Monad Algebraic Data Type
Philip Schwarz
 
First-Class Patterns
John De Goes
 
Scala jargon cheatsheet
Ruslan Shevchenko
 
Procedure Typing for Scala
akuklev
 
Option, Either, Try and what to do with corner cases when they arise
Michal Bigos
 
Ad

Recently uploaded (20)

PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PPTX
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
Ad

Funcional Programming in Scala - Chapter 4