SlideShare une entreprise Scribd logo
1  sur  76
Télécharger pour lire hors ligne
Agenda
• Thinking less with Functions
• Thinking less with Data
• Thinking less with Patterns
• Summary
• Questions
Function Signatures
Visualizing
def f(i: Int): String = ???
Types and values
def f(i: Int): String = ???
Function
def f(i: Int): String = ???
Visualizing the function signature
def divide(a: Int, b: Int): Int = {
if(b == 0)
throw new ArithmeticException("/ by zero")
a / b
}
Feed it 0
divide(2, 0)
Bad function!
scala> divide(2, 0)
java.lang.ArithmeticException: / by zero
at .divide(<console>:14)
... 54 elided
Bad function
signature!
def divide(a: Int, b: Int): Int = {
if(b == 0)
throw new ArithmeticException("/ by zero")
a / b
}
Total Function
def inc(x: Int): Int = x+1
Pure Function
def inc(x: Int): Int = {
println("a")
x+1
}
Convert divide to a total function
case class DivisionError(e: String)
def divide(a: Int, b: Int): Either[DivisionError, Int] = {
if( b == 0) Left(DivisionError("/ by zero"))
Right(a / b)
}
Container[_] types
Option[_]: Returning no value is
possible
def getMiddleName(name: String):
Option[String] = ???
Either[_,_]: Possibility of errors
def divide(a: Int, b: Int):
Either[String, Int] = ???
List[_]: Multiple outputs
def getHosts(mongoUri: String):
List[String] = ???
Future[_]: When?
def loadUser(id: String):
Future[String] = ???
Recap: Function signatures
• Total functions have a corresponding
output value to each input value
• Pure functions have no side effects
• Enrich your signature with Container
types
Good function signatures:
Think less
About implementation
Parametric
Functions
Let's play a game!
How many implementations does
this function have?
def foo[A](a: A): A = ???
How many implementations does
this function have?
def foo[A](a: A): A = a
How many implementations does
this function have?
def foo[A](as: List[A]): List[A] = ???
How many implementation does
this function have?
def foo[A](as: List[A]): List[A] = {
if(as.isEmpty) Nil
else as.head :: Nil
}
def foo[A](as: List[A]): List[A] = {
if(as.isEmpty) Nil
else as.tail.toList.head :: Nil
}
How many implementation does
this function have?
def foo[A](a: A): Int = ???
How many implementation does
this function have?
def foo[A](a: A): Int = 1
def foo[A](a: A): Int = 2
def foo[A](a: A): Int = 3
def foo[A](a: A): Int = 4
Recap: parametric
functions
• Increase abstraction,
Lower implementation
space
• This notion is called
parametricity
Parametricity:
Think less
About your implementation
Beyond Functions
What about real design?
• Design data
• Design behavior
Encoding data using
Sum Types
Encoding data using Sum Types
sealed trait Developer
case class DevOps(name: String) extends Developer
case class FullStack(name: String, salary: Int) extends Developer
case class ML(name: String, salary: Long) extends Developer
Exhaustiveness Checking on ADTs
scala> def sayHi(d: Developer) = d match {
|
| case DevOps(n) => s"Hi $n !"
|
| case FullStack(n, _) => s"Hi $n !"
|
| }
<console>:17: warning: match may not be exhaustive.
It would fail on the following input: ML(_, _)
def sayHi(d: Developer) = d match {
^
sayHi: (d: Developer)String
String email or id?
case class User(
id: String,
email: String,
created: Long
)
def loadUser(user: String): User = ???
Encode your domain with Value
classes
case class Id(id: String) extends AnyVal
case class Email(email: String) extends AnyVal
case class TimeStamp(ts: Long) extends AnyVal
case class User(id: Id, email: Email, created: TimeStamp)
def loadUser(user: Email): User = ???
Recap: ADTs / Value classes
• ADTs encode immutable data
• Value classes let you add domain
knowledge
ADTs/Value classes:
Think less
About Correctness
Patterns
Fit this in your mental
stack!
Patterns at the function level
1 + 1
"a".concat("b")
List(1,2,3) ++ List(4,5,6)
Refactoring to a common shape
def combineInts(x: Int, y: Int): Int = ???
def combineString(x: String, y: String): String = ???
def combineLists[A](x: List[A], y: List[A]): List[A] = ???
Extracting to typeclasses
trait Combinable[A] {
def combine(x: A, y: A): A
}
You can implement
instances
Combinable[Int]
implicit val combineInts =
new Combinable[Int] {
def combine(x: Int, y: Int): Int =
x + y
}
Combinable[String]
implicit val combineStrings =
new Combinable[String] {
def combine(x: String, y: String): String =
x + y
}
Combinable[Foo]
case class Foo(i: Int, s: String)
implicit val combineFoos =
new Combinable[Foo] {
def combine(x: Foo, y: Foo): Foo =
Foo(x.i + y.i , x.s.concat(y.s))
}
Use the instances
scala> implicitly[Combinable[String]].combine("a", "b")
res6: String = ab
scala> implicitly[Combinable[Int]].combine(1, 1)
res7: Int = 2
scala> implicitly[Combinable[Foo]].combine(Foo(1,"a"), Foo(2,"b"))
res8: Foo = Foo(3,ab)
Nice Refactoring!
Did we gain
anything?
Combinable[A]
trait Combinable[A] {
def combine(x: A, y: A): A
}
Monoid[A]
trait Monoid[A] {
def empty: A
def combine(x: A, y: A): A
}
Or you can just import
Monoid
And get free
instances!
import cats.Monoid
import cats.instances.all._
import cats.syntax.monoid._
scala> 1.combine(1)
res9: Int = 2
scala> "a".combine("b")
res10: String = ab
scala> List(1,2,3).combine(List(4,5,6))
res11: List[Int] = List(1, 2, 3, 4, 5, 6)
"Everything sufficiently
polymorphic and useful
already exists"
-- Rob Norris @tpolecat
More Free code!
import cats.derived.MkMonoid._
scala> Foo(1,"a").combine(Foo(2,"b"))
res12: Foo = Foo(3,ab)
combineAll is a derived function
trait Monoid[A] {
def empty: A
def combine(x: A, y: A): A
def combineAll(as: List[A]): A =
as.foldLeft(empty)(combine)
}
scala> Monoid[Int].combineAll(List(1,2,3))
res13: Int = 6
scala> Monoid[String].combineAll(List("a","b","c"))
res14: String = abc
scala> Monoid[Foo].combineAll(Set(Foo(1,"a"),Foo(2,"b"),Foo(3,"c")))
res15: Foo = Foo(6,abc)
Recap: Functional Patterns
• Functional patterns are encoded as typecalsses
• As opposed to Design patterns, functional
patterns are encoded as libraries like cats
• They come with free implementations
• Ecosystem of libraries working with these
patterns
One very important thing
• As opposed to frameworks, functional
patterns knowledge does not get old
• You can carry with you through your career,
regardless of languages or platforms
• Some languages even support them out of
the box λ
Summary
• Function Signatures with total/pure
functions and container types
• Data encoded with sealed trait/case
classes
• Patterns at the function level with
typeclasses using parametric functions
Questions
Thank You
@dsebban
Come chat about FP @ BigPanda
booth
Scalapeno18 - Thinking Less with Scala

Contenu connexe

Tendances

Reasoning about laziness
Reasoning about lazinessReasoning about laziness
Reasoning about laziness
Johan Tibell
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
PTCL
 
Computer Practical
Computer PracticalComputer Practical
Computer Practical
PLKFM
 

Tendances (20)

OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1
 
Testing in the World of Functional Programming
Testing in the World of Functional ProgrammingTesting in the World of Functional Programming
Testing in the World of Functional Programming
 
Python programming workshop session 1
Python programming workshop session 1Python programming workshop session 1
Python programming workshop session 1
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and Cats
 
Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...
 
Reasoning about laziness
Reasoning about lazinessReasoning about laziness
Reasoning about laziness
 
Contravariant functors in scala
Contravariant functors in scalaContravariant functors in scala
Contravariant functors in scala
 
Why functional programming and category theory strongly matters
Why functional programming and category theory strongly mattersWhy functional programming and category theory strongly matters
Why functional programming and category theory strongly matters
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3
 
Abstracting over Execution with Higher Kinded Types
Abstracting over Execution with Higher Kinded TypesAbstracting over Execution with Higher Kinded Types
Abstracting over Execution with Higher Kinded Types
 
Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)
 
Monad Transformers - Part 1
Monad Transformers - Part 1Monad Transformers - Part 1
Monad Transformers - Part 1
 
Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Python programming- Part IV(Functions)
Python programming- Part IV(Functions)
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Arrays
ArraysArrays
Arrays
 
Computer Practical
Computer PracticalComputer Practical
Computer Practical
 
A taste of Functional Programming
A taste of Functional ProgrammingA taste of Functional Programming
A taste of Functional Programming
 
USER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHONUSER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHON
 
DATA TYPE IN PYTHON
DATA TYPE IN PYTHONDATA TYPE IN PYTHON
DATA TYPE IN PYTHON
 

Similaire à Scalapeno18 - Thinking Less with Scala

Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
Mario Fusco
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
pramode_ce
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional Programming
Eelco Visser
 

Similaire à Scalapeno18 - Thinking Less with Scala (20)

Power of functions in a typed world
Power of functions in a typed worldPower of functions in a typed world
Power of functions in a typed world
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Matters
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
 
C# programming
C# programming C# programming
C# programming
 
Scala best practices
Scala best practicesScala best practices
Scala best practices
 
Generic Functional Programming with Type Classes
Generic Functional Programming with Type ClassesGeneric Functional Programming with Type Classes
Generic Functional Programming with Type Classes
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharp
 
GoLightly: Building VM-Based Language Runtimes with Google Go
GoLightly: Building VM-Based Language Runtimes with Google GoGoLightly: Building VM-Based Language Runtimes with Google Go
GoLightly: Building VM-Based Language Runtimes with Google Go
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patterns
 
An Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using HaskellAn Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using Haskell
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional Programming
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
Talk - Query monad
Talk - Query monad Talk - Query monad
Talk - Query monad
 

Dernier

notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Dernier (20)

Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 

Scalapeno18 - Thinking Less with Scala