SlideShare a Scribd company logo
Why	
  Func)onal	
  Programming	
  
is	
  Worth	
  the	
  Effort	
  
Contents	
  
•  Benefits	
  of	
  func)onal	
  programming	
  
•  Focus	
  on	
  reliability	
  
•  Examples:	
  Using	
  the	
  right	
  types	
  
•  Func)onal	
  Programming	
  in	
  Industry	
  
•  Live	
  Coding	
  in	
  Scala	
  
Audience	
  Polls	
  
I	
  am	
  most	
  experienced	
  in:	
  
●  Java	
  
●  C/C++/Obj-­‐C/C#	
  
●  PHP	
  
●  VB	
  
●  Python	
  
●  JavaScript	
  
●  Ruby	
  
●  Perl	
  
●  Racket	
  
●  Other	
  
Audience	
  Polls	
  
My	
  programs	
  work	
  perfectly	
  the	
  first	
  )me	
  I	
  run	
  them:	
  
	
  
1.  Nearly	
  every	
  )me	
  
2.  Frequently	
  
3.  Some)mes	
  
4.  Rarely	
  
5.  Almost	
  Never	
  
	
  
What	
  is	
  Func)onal	
  Programming?	
  
●  Computa)on	
  as	
  evalua)on	
  of	
  func)ons	
  
o  No	
  side-­‐effects	
  (pure	
  func)ons)	
  
	
  
Consequences	
  of	
  Purity	
  
●  FP	
  requires	
  higher	
  order	
  func)ons	
  
●  Order	
  of	
  evalua)on	
  has	
  no	
  impact	
  on	
  
returned	
  value	
  (as	
  in	
  mathema)cs)	
  
	
  
Evalua)on	
  Order:	
  Illustra)on	
  
Given	
  the	
  func)on	
  square(x)	
  =	
  x*x	
  
square(max(3,	
  4))	
   max(3,	
  4)	
  *	
  max(3,	
  4)	
  
square	
  (4)	
   4	
  *	
  4	
  
Benefits	
  
●  Clarity	
  
o  Referen)al	
  transparency	
  facilitates	
  reasoning	
  
●  Conciseness	
  
o  HOFs	
  enable	
  abstrac)on	
  and	
  favor	
  reuse	
  
●  Safety	
  
o  Effects	
  are	
  values	
  and	
  can	
  be	
  type	
  checked!	
  
●  Maintainability	
  
o  	
  As	
  a	
  consequence	
  
More	
  Benefits	
  
●  Concurrency	
  
	
  
Determinis)c	
  Parallelism:	
  
Order	
  of	
  evalua)on	
  is	
  irrelevant,	
  so	
  parallel	
  
evalua)on	
  is	
  fine	
  too.	
  
Type	
  Checking	
  
Type:	
  Essen)ally,	
  a	
  set	
  of	
  admissible	
  values	
  for	
  a	
  
variable,	
  a	
  parameter,	
  a	
  field	
  or	
  a	
  return	
  value.	
  
	
  
When	
  designing	
  a	
  program	
  we	
  make	
  decisions	
  
about	
  types.	
  We	
  should	
  tell	
  the	
  compiler!	
  
	
  
Type	
  inference	
  means	
  you	
  don’t	
  have	
  to	
  repeat	
  
types	
  constantly	
  to	
  the	
  compiler.	
  It	
  reasons	
  about	
  
types,	
  it	
  doesn’t	
  just	
  check	
  them.	
  
	
  
You	
  can	
  do	
  pair	
  programming	
  with	
  the	
  compiler.	
  
	
  
What	
  about	
  Dynamic	
  Type	
  Checking	
  
Almost	
  no	
  type	
  checking;	
  	
  
only	
  primi)ve	
  func)ons	
  are	
  checked.	
  
Adding	
  run)me	
  checks	
  in	
  user	
  func)ons	
  is	
  possible	
  but	
  
is	
  slow	
  and	
  is	
  more	
  trouble	
  than	
  sta)c	
  checking.	
  
	
  
Failed	
  checks	
  require	
  )me-­‐consuming	
  diagnosis	
  
with	
  debuggers.	
  
Classes	
  of	
  Bugs	
  
Es)mates	
  for	
  illustra)on	
  purposes.	
  No	
  real	
  data	
  behind	
  this.	
  
Extreme	
  Reliability	
  
	
  
When	
  you	
  play	
  by	
  the	
  rules	
  of	
  
type	
  system	
  you	
  avoid	
  almost	
  
all	
  errors.	
  
	
  
Once	
  it	
  compiles,	
  it	
  works!	
  
	
  
	
  
Curry-­‐Howard	
  Correspondence	
  
Equivalence	
  between	
  
●  Proofs	
  (in	
  some	
  logic)	
  
●  Programs	
  (in	
  some	
  typed	
  FP	
  language)	
  
Dependently	
  Typed	
  Languages	
  
●  Agda	
  
●  Idris	
  	
  
Types	
  vs	
  Tests	
  
Tests	
  check	
  a	
  few	
  execu)ons	
  
	
  
Types	
  check	
  all	
  poten)al	
  execu)ons!	
  
Test	
  Driven	
  to	
  Type	
  Driven	
  Development	
  
Test	
  Driven	
  Development	
  has	
  benefits	
  but	
  also	
  
important	
  costs.	
  With	
  the	
  safety	
  brought	
  by	
  
types,	
  many	
  tests	
  are	
  no	
  longer	
  required.	
  
	
  
Tests	
  can	
  become	
  stale.	
  
	
  
Some	
  automated	
  tests	
  are	
  s)ll	
  useful.	
  
Documenta)on	
  is	
  Brihle	
  
Change	
  is	
  omnipresent	
  and	
  documenta)on	
  will	
  
frequently	
  become	
  stale.	
  
	
  
More	
  precise	
  types	
  can	
  act	
  as	
  documenta)on	
  
that	
  cannot	
  be	
  out	
  of	
  date.	
  
	
  
So	
  you	
  might	
  need	
  a	
  bit	
  less	
  documenta)on.	
  
My	
  Exposure	
  to	
  Func)onal	
  Programming	
  
In	
  1992:	
  Miranda	
  
	
  
	
  	
  Was	
  told	
  not	
  to	
  expect	
  to	
  write	
  real	
  sojware	
  
with	
  that,	
  more	
  of	
  a	
  toy	
  language.	
  
	
  
Func)onal	
  Programming	
  is	
  no	
  longer	
  a	
  toy.	
  
Func)onal	
  Programming	
  in	
  Industry	
  
●  Not	
  yet	
  mainstream,	
  but	
  seeing	
  more	
  
adop)on	
  
●  Many	
  ecosystems	
  sufficiently	
  mature	
  
	
  
	
  
	
  
Func)onal	
  Programming	
  Languages	
  in	
  Industry	
  
●  Dynamically	
  Typed	
  
o  Scheme,	
  Racket,	
  Common	
  Lisp,	
  Clojure,	
  Erlang	
  	
  
●  Typed	
  
o  Haskell	
  
o  F#	
  
o  Scala	
  
o  OCaml	
  
	
  
Func)onal	
  Programming	
  at	
  BoldRadius	
  
●  Scala	
  is	
  our	
  main	
  JVM	
  language	
  
●  .NET	
  developers	
  looking	
  seriously	
  at	
  F#	
  
Using	
  the	
  Right	
  Types	
  
The	
  right	
  type	
  is	
  
●  not	
  too	
  strict	
  (strong	
  formula)	
  
●  not	
  too	
  loose	
  (weak	
  formula)	
  
	
  
Too	
  Strict	
  	
  
Producer	
  works	
  too	
  hard	
  to	
  produce	
  
informa)on	
  the	
  consumer	
  does	
  not	
  need.	
  
	
  
●  Extraneous	
  fields	
  (conjunc)ons,	
  “and”)	
  
●  Not	
  enough	
  alterna)ves	
  (disjuc)ons,	
  “or”)	
  
Too	
  Loose	
  
Consumer	
  is	
  missing	
  informa)on	
  that	
  the	
  
Producer	
  could	
  easily	
  have	
  provided.	
  
	
  
●  Missing	
  fields	
  (conjunc)ons,	
  “and”)	
  
●  Too	
  many	
  alterna)ves	
  (disjuc)ons,	
  “or”)	
  
	
  
	
  
Avoid	
  Trying	
  to	
  Future	
  Proof	
  
Building	
  a	
  type	
  for	
  the	
  future	
  ojen	
  makes	
  it	
  too	
  
strong	
  or	
  too	
  weak	
  for	
  the	
  current	
  producers	
  
and	
  consumers.	
  
	
  
Extreme	
  examples:	
  	
  
●  “Stringly	
  typed”	
  code	
  encodes	
  everything	
  in	
  strings.	
  
●  Using	
  Object	
  in	
  Java	
  
	
  
Type	
  Change	
  
As	
  needs	
  change,	
  both	
  types	
  and	
  func)ons	
  
should	
  change.	
  
	
  
Compiler	
  errors	
  will	
  indicate	
  inconsistencies	
  
during	
  refactoring.	
  
	
  
Persisted	
  data	
  increases	
  fric)on	
  however.	
  
	
  
Examples	
  
Op)onal	
  Values	
  
Dis)nguish	
  what	
  is	
  op)onal	
  and	
  required.	
  
Null	
  references	
  should	
  be	
  banned	
  because	
  they	
  can	
  be	
  
assigned	
  to	
  all	
  types,	
  making	
  everything	
  op)onal.	
  	
  
data Maybe a = Nothing | Just a
	
  
data Car = Car Engine Wheels (Maybe AC)
Car	
  consumers	
  can	
  rely	
  on	
  the	
  presence	
  of	
  an	
  Engine.	
  
	
  
Non	
  Applicable	
  Field	
  
data Account = Account {open :: Boolean,
balance :: Dollars}
	
  
Too	
  loose:	
  
●  We	
  want	
  to	
  disallow	
  a	
  closed	
  account	
  with	
  a	
  non-­‐
zero	
  balance	
  
	
  
data Account2 =
Open {balance :: Dollars}
| Closed
Duplicate	
  Data	
  	
  
data AgeInfo = AgeInfo {age: Int, minor:
Boolean}
	
  
Too	
  loose:	
  
●  Consumers	
  may	
  rely	
  on	
  these	
  two	
  fields	
  having	
  
consistent	
  values.	
  
	
  
data AgeInfo2 = AgeInfo2 {age: Int}
isMinor :: AgeInfo2 -> Boolean
	
  
List	
  with	
  a	
  Selected	
  Element	
  
data Selected1 a = Selected1 (List a) Int
	
  
Too	
  loose:	
  
●  Int	
  index	
  could	
  be	
  out	
  of	
  range	
  
●  List	
  could	
  be	
  empty	
  
Consumer	
  may	
  get	
  input	
  it	
  does	
  know	
  how	
  to	
  
handle.	
  
	
  
data Selected2 a = Selected2 (List a) a (List a)
	
  
Binary	
  Tree	
  
data Node i a = Node i a a
data Tree i a =
Leaf a
| Internal (Node i (Tree i a))
	
  
What	
  if	
  we	
  needed	
  to	
  represent	
  a	
  full	
  binary	
  tree?	
  	
  
●  Could	
  use	
  Tree	
  and	
  maintain	
  the	
  “fullness”	
  invariant	
  
at	
  run)me.	
  Error	
  prone.	
  
●  Or	
  use	
  the	
  right	
  type	
  that	
  checks	
  this	
  at	
  compile	
  
)me...	
  
	
  
Full	
  Binary	
  Tree	
  (non-­‐regular	
  type!)	
  
data Node i a = Node i a a
data Full i a =
Last a
| Level (Full i (Node i a))
	
  
a	
  |	
  Node i a	
  |	
  Node i (Node i a)	
  |	
  ...	
  
a	
  |	
  (i, a, a)	
  |	
  (i, (i, a, a), (i, a, a))	
  |	
  ...	
  
Free	
  Monad 	
  	
  
data Free f a = Pure a | Roll (f (Free f a))
Equivalent	
  to	
  this:	
  a | f (a | f ( a | … ))
Not	
  this:	
  a | f a | f (f a) | ...
data Node i a = Node i a a
Free (Node i) a	
  is	
  equivalent	
  to	
  Tree i a, not Full i a
More	
  Free	
  Monads	
  
data Command a =
WriteLine String a
| ReadLine (String -> a)
Free Command a =
a | Command (a | Command (a | Command (...)))
Represents	
  computa)ons	
  doing	
  specific	
  IO	
  opera)ons	
  in	
  a	
  type-­‐safe	
  
manner!	
  
Patrick	
  Prémont	
  
Func)onal	
  Programming	
  Architect	
  
BoldRadius	
  Solu)ons	
  
@ppremont	
  
www.boldradius.com	
  

More Related Content

What's hot (20)

PPTX
C++ Basics introduction to typecasting Webinar Slides 1
Ali Raza Jilani
 
PPT
What To Expect With C
Joseph Guadagno
 
PDF
Pigaios: A Tool for Diffing Source Codes against Binaries (Hacktivity 2018)
Joxean Koret
 
PPTX
C languaGE UNIT-1
Malikireddy Bramhananda Reddy
 
PPTX
Procedural programming
Anbarasan Gangadaran
 
PPT
Intro. to prog. c++
KurdGul
 
PPTX
Type checking compiler construction Chapter #6
Daniyal Mughal
 
PPTX
introduction to Python | Part 1
Ahmedalhassar1
 
PDF
Functional programming in scala
Stratio
 
PPTX
Functional programming
PiumiPerera7
 
PPTX
C programming interview questions
adarshynl
 
PPT
C programming for Computing Techniques
Appili Vamsi Krishna
 
PPTX
Dart PPT.pptx
DSCMESCOE
 
PDF
Dart the better Javascript 2015
Jorg Janke
 
PPTX
Dart programming language
Aniruddha Chakrabarti
 
PDF
Python Programming - III. Controlling the Flow
Ranel Padon
 
PPT
Programming of c++
Ateeq Sindhu
 
PDF
Introduction to c++ ppt 1
Prof. Dr. K. Adisesha
 
PDF
Implementing Higher-Kinded Types in Dotty
Martin Odersky
 
PDF
Dart workshop
Vishnu Suresh
 
C++ Basics introduction to typecasting Webinar Slides 1
Ali Raza Jilani
 
What To Expect With C
Joseph Guadagno
 
Pigaios: A Tool for Diffing Source Codes against Binaries (Hacktivity 2018)
Joxean Koret
 
Procedural programming
Anbarasan Gangadaran
 
Intro. to prog. c++
KurdGul
 
Type checking compiler construction Chapter #6
Daniyal Mughal
 
introduction to Python | Part 1
Ahmedalhassar1
 
Functional programming in scala
Stratio
 
Functional programming
PiumiPerera7
 
C programming interview questions
adarshynl
 
C programming for Computing Techniques
Appili Vamsi Krishna
 
Dart PPT.pptx
DSCMESCOE
 
Dart the better Javascript 2015
Jorg Janke
 
Dart programming language
Aniruddha Chakrabarti
 
Python Programming - III. Controlling the Flow
Ranel Padon
 
Programming of c++
Ateeq Sindhu
 
Introduction to c++ ppt 1
Prof. Dr. K. Adisesha
 
Implementing Higher-Kinded Types in Dotty
Martin Odersky
 
Dart workshop
Vishnu Suresh
 

Viewers also liked (20)

PDF
Code Brevity in Scala
BoldRadius Solutions
 
PDF
String Interpolation in Scala | BoldRadius
BoldRadius Solutions
 
PDF
Partial Functions in Scala
BoldRadius Solutions
 
PPTX
Presentation1
Adlu Panser Brutal
 
PPTX
Pow séminaire "Divine Protection"
Alkauthar
 
PDF
Pattern Matching in Scala
BoldRadius Solutions
 
PDF
Scala: Collections API
BoldRadius Solutions
 
PPTX
Test
DDgmorrison1
 
PDF
What Are For Expressions in Scala?
BoldRadius Solutions
 
PPTX
Curriculum
thipik
 
PDF
Empirics of standard deviation
Adebanji Ayeni
 
PPTX
Value Classes in Scala | BoldRadius
BoldRadius Solutions
 
PDF
Taste of korea
Marta Garcia Trincado
 
PPTX
Cold drawn guide rail, Elevator guide Rail India
N Liftee
 
PPTX
Paul Partlow Highlighted Portfolio
Paul Partlow
 
PPT
Earth moon 1
TrapQveen Mia
 
PDF
Scala Days Highlights | BoldRadius
BoldRadius Solutions
 
PDF
Punishment Driven Development #agileinthecity
Louise Elliott
 
PDF
Why Not Make the Transition from Java to Scala?
BoldRadius Solutions
 
PDF
Immutability in Scala
BoldRadius Solutions
 
Code Brevity in Scala
BoldRadius Solutions
 
String Interpolation in Scala | BoldRadius
BoldRadius Solutions
 
Partial Functions in Scala
BoldRadius Solutions
 
Presentation1
Adlu Panser Brutal
 
Pow séminaire "Divine Protection"
Alkauthar
 
Pattern Matching in Scala
BoldRadius Solutions
 
Scala: Collections API
BoldRadius Solutions
 
What Are For Expressions in Scala?
BoldRadius Solutions
 
Curriculum
thipik
 
Empirics of standard deviation
Adebanji Ayeni
 
Value Classes in Scala | BoldRadius
BoldRadius Solutions
 
Taste of korea
Marta Garcia Trincado
 
Cold drawn guide rail, Elevator guide Rail India
N Liftee
 
Paul Partlow Highlighted Portfolio
Paul Partlow
 
Earth moon 1
TrapQveen Mia
 
Scala Days Highlights | BoldRadius
BoldRadius Solutions
 
Punishment Driven Development #agileinthecity
Louise Elliott
 
Why Not Make the Transition from Java to Scala?
BoldRadius Solutions
 
Immutability in Scala
BoldRadius Solutions
 
Ad

Similar to Functional Programming - Worth the Effort (20)

PDF
Can functional programming be liberated from static typing?
Vsevolod Dyomkin
 
PDF
Thinking Functionally
Piyush Katariya
 
KEY
Scala: functional programming for the imperative mind
Sander Mak (@Sander_Mak)
 
PDF
OOP and FP
Mario Fusco
 
KEY
LISP: How I Learned To Stop Worrying And Love Parantheses
Dominic Graefen
 
PPTX
The joy of functional programming
Steve Zhang
 
PPTX
Scala, Play 2.0 & Cloud Foundry
Pray Desai
 
PPTX
Functional programming with FSharp
Daniele Pozzobon
 
PPTX
a brief explanation on the topic of Imperative Programming Paradigm.pptx
sajit20
 
PPTX
Introduction to Functional Programming
Dave Fancher
 
PPT
Functional Programming - Past, Present and Future
Pushkar Kulkarni
 
PPT
Functional Programming Past Present Future
IndicThreads
 
PPTX
F# for startups
joelgrus
 
PDF
The Fuss about || Haskell | Scala | F# ||
Ashwin Rao
 
PPTX
F# for startups v2
joelgrus
 
PDF
Functional programming is the most extreme programming
samthemonad
 
PDF
Beyond PITS, Functional Principles for Software Architecture
Jayaram Sankaranarayanan
 
PDF
Functional programming with F#
Remik Koczapski
 
PDF
Gentle Introduction to Scala
Fangda Wang
 
PDF
A Functional Approach to Java: Augmenting Object-Oriented Java Code with Func...
romergalbowx
 
Can functional programming be liberated from static typing?
Vsevolod Dyomkin
 
Thinking Functionally
Piyush Katariya
 
Scala: functional programming for the imperative mind
Sander Mak (@Sander_Mak)
 
OOP and FP
Mario Fusco
 
LISP: How I Learned To Stop Worrying And Love Parantheses
Dominic Graefen
 
The joy of functional programming
Steve Zhang
 
Scala, Play 2.0 & Cloud Foundry
Pray Desai
 
Functional programming with FSharp
Daniele Pozzobon
 
a brief explanation on the topic of Imperative Programming Paradigm.pptx
sajit20
 
Introduction to Functional Programming
Dave Fancher
 
Functional Programming - Past, Present and Future
Pushkar Kulkarni
 
Functional Programming Past Present Future
IndicThreads
 
F# for startups
joelgrus
 
The Fuss about || Haskell | Scala | F# ||
Ashwin Rao
 
F# for startups v2
joelgrus
 
Functional programming is the most extreme programming
samthemonad
 
Beyond PITS, Functional Principles for Software Architecture
Jayaram Sankaranarayanan
 
Functional programming with F#
Remik Koczapski
 
Gentle Introduction to Scala
Fangda Wang
 
A Functional Approach to Java: Augmenting Object-Oriented Java Code with Func...
romergalbowx
 
Ad

More from BoldRadius Solutions (9)

PDF
Introduction to the Typesafe Reactive Platform
BoldRadius Solutions
 
PDF
Towards Reliable Lookups - Scala By The Bay
BoldRadius Solutions
 
PDF
How You Convince Your Manager To Adopt Scala.js in Production
BoldRadius Solutions
 
PDF
Introduction to the Actor Model
BoldRadius Solutions
 
PPTX
Domain Driven Design Through Onion Architecture
BoldRadius Solutions
 
PDF
What are Sealed Classes in Scala?
BoldRadius Solutions
 
PDF
How To Use Higher Order Functions in Scala
BoldRadius Solutions
 
PDF
Scala Days 2014: Pitching Typesafe
BoldRadius Solutions
 
PDF
Demonstrating Case Classes in Scala
BoldRadius Solutions
 
Introduction to the Typesafe Reactive Platform
BoldRadius Solutions
 
Towards Reliable Lookups - Scala By The Bay
BoldRadius Solutions
 
How You Convince Your Manager To Adopt Scala.js in Production
BoldRadius Solutions
 
Introduction to the Actor Model
BoldRadius Solutions
 
Domain Driven Design Through Onion Architecture
BoldRadius Solutions
 
What are Sealed Classes in Scala?
BoldRadius Solutions
 
How To Use Higher Order Functions in Scala
BoldRadius Solutions
 
Scala Days 2014: Pitching Typesafe
BoldRadius Solutions
 
Demonstrating Case Classes in Scala
BoldRadius Solutions
 

Recently uploaded (20)

PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Digital Circuits, important subject in CS
contactparinay1
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 

Functional Programming - Worth the Effort

  • 1. Why  Func)onal  Programming   is  Worth  the  Effort  
  • 2. Contents   •  Benefits  of  func)onal  programming   •  Focus  on  reliability   •  Examples:  Using  the  right  types   •  Func)onal  Programming  in  Industry   •  Live  Coding  in  Scala  
  • 3. Audience  Polls   I  am  most  experienced  in:   ●  Java   ●  C/C++/Obj-­‐C/C#   ●  PHP   ●  VB   ●  Python   ●  JavaScript   ●  Ruby   ●  Perl   ●  Racket   ●  Other  
  • 4. Audience  Polls   My  programs  work  perfectly  the  first  )me  I  run  them:     1.  Nearly  every  )me   2.  Frequently   3.  Some)mes   4.  Rarely   5.  Almost  Never    
  • 5. What  is  Func)onal  Programming?   ●  Computa)on  as  evalua)on  of  func)ons   o  No  side-­‐effects  (pure  func)ons)    
  • 6. Consequences  of  Purity   ●  FP  requires  higher  order  func)ons   ●  Order  of  evalua)on  has  no  impact  on   returned  value  (as  in  mathema)cs)    
  • 7. Evalua)on  Order:  Illustra)on   Given  the  func)on  square(x)  =  x*x   square(max(3,  4))   max(3,  4)  *  max(3,  4)   square  (4)   4  *  4  
  • 8. Benefits   ●  Clarity   o  Referen)al  transparency  facilitates  reasoning   ●  Conciseness   o  HOFs  enable  abstrac)on  and  favor  reuse   ●  Safety   o  Effects  are  values  and  can  be  type  checked!   ●  Maintainability   o   As  a  consequence  
  • 9. More  Benefits   ●  Concurrency     Determinis)c  Parallelism:   Order  of  evalua)on  is  irrelevant,  so  parallel   evalua)on  is  fine  too.  
  • 10. Type  Checking   Type:  Essen)ally,  a  set  of  admissible  values  for  a   variable,  a  parameter,  a  field  or  a  return  value.     When  designing  a  program  we  make  decisions   about  types.  We  should  tell  the  compiler!     Type  inference  means  you  don’t  have  to  repeat   types  constantly  to  the  compiler.  It  reasons  about   types,  it  doesn’t  just  check  them.     You  can  do  pair  programming  with  the  compiler.    
  • 11. What  about  Dynamic  Type  Checking   Almost  no  type  checking;     only  primi)ve  func)ons  are  checked.   Adding  run)me  checks  in  user  func)ons  is  possible  but   is  slow  and  is  more  trouble  than  sta)c  checking.     Failed  checks  require  )me-­‐consuming  diagnosis   with  debuggers.  
  • 12. Classes  of  Bugs   Es)mates  for  illustra)on  purposes.  No  real  data  behind  this.  
  • 13. Extreme  Reliability     When  you  play  by  the  rules  of   type  system  you  avoid  almost   all  errors.     Once  it  compiles,  it  works!      
  • 14. Curry-­‐Howard  Correspondence   Equivalence  between   ●  Proofs  (in  some  logic)   ●  Programs  (in  some  typed  FP  language)  
  • 15. Dependently  Typed  Languages   ●  Agda   ●  Idris    
  • 16. Types  vs  Tests   Tests  check  a  few  execu)ons     Types  check  all  poten)al  execu)ons!  
  • 17. Test  Driven  to  Type  Driven  Development   Test  Driven  Development  has  benefits  but  also   important  costs.  With  the  safety  brought  by   types,  many  tests  are  no  longer  required.     Tests  can  become  stale.     Some  automated  tests  are  s)ll  useful.  
  • 18. Documenta)on  is  Brihle   Change  is  omnipresent  and  documenta)on  will   frequently  become  stale.     More  precise  types  can  act  as  documenta)on   that  cannot  be  out  of  date.     So  you  might  need  a  bit  less  documenta)on.  
  • 19. My  Exposure  to  Func)onal  Programming   In  1992:  Miranda        Was  told  not  to  expect  to  write  real  sojware   with  that,  more  of  a  toy  language.     Func)onal  Programming  is  no  longer  a  toy.  
  • 20. Func)onal  Programming  in  Industry   ●  Not  yet  mainstream,  but  seeing  more   adop)on   ●  Many  ecosystems  sufficiently  mature        
  • 21. Func)onal  Programming  Languages  in  Industry   ●  Dynamically  Typed   o  Scheme,  Racket,  Common  Lisp,  Clojure,  Erlang     ●  Typed   o  Haskell   o  F#   o  Scala   o  OCaml    
  • 22. Func)onal  Programming  at  BoldRadius   ●  Scala  is  our  main  JVM  language   ●  .NET  developers  looking  seriously  at  F#  
  • 23. Using  the  Right  Types   The  right  type  is   ●  not  too  strict  (strong  formula)   ●  not  too  loose  (weak  formula)    
  • 24. Too  Strict     Producer  works  too  hard  to  produce   informa)on  the  consumer  does  not  need.     ●  Extraneous  fields  (conjunc)ons,  “and”)   ●  Not  enough  alterna)ves  (disjuc)ons,  “or”)  
  • 25. Too  Loose   Consumer  is  missing  informa)on  that  the   Producer  could  easily  have  provided.     ●  Missing  fields  (conjunc)ons,  “and”)   ●  Too  many  alterna)ves  (disjuc)ons,  “or”)      
  • 26. Avoid  Trying  to  Future  Proof   Building  a  type  for  the  future  ojen  makes  it  too   strong  or  too  weak  for  the  current  producers   and  consumers.     Extreme  examples:     ●  “Stringly  typed”  code  encodes  everything  in  strings.   ●  Using  Object  in  Java    
  • 27. Type  Change   As  needs  change,  both  types  and  func)ons   should  change.     Compiler  errors  will  indicate  inconsistencies   during  refactoring.     Persisted  data  increases  fric)on  however.    
  • 29. Op)onal  Values   Dis)nguish  what  is  op)onal  and  required.   Null  references  should  be  banned  because  they  can  be   assigned  to  all  types,  making  everything  op)onal.     data Maybe a = Nothing | Just a   data Car = Car Engine Wheels (Maybe AC) Car  consumers  can  rely  on  the  presence  of  an  Engine.    
  • 30. Non  Applicable  Field   data Account = Account {open :: Boolean, balance :: Dollars}   Too  loose:   ●  We  want  to  disallow  a  closed  account  with  a  non-­‐ zero  balance     data Account2 = Open {balance :: Dollars} | Closed
  • 31. Duplicate  Data     data AgeInfo = AgeInfo {age: Int, minor: Boolean}   Too  loose:   ●  Consumers  may  rely  on  these  two  fields  having   consistent  values.     data AgeInfo2 = AgeInfo2 {age: Int} isMinor :: AgeInfo2 -> Boolean  
  • 32. List  with  a  Selected  Element   data Selected1 a = Selected1 (List a) Int   Too  loose:   ●  Int  index  could  be  out  of  range   ●  List  could  be  empty   Consumer  may  get  input  it  does  know  how  to   handle.     data Selected2 a = Selected2 (List a) a (List a)  
  • 33. Binary  Tree   data Node i a = Node i a a data Tree i a = Leaf a | Internal (Node i (Tree i a))   What  if  we  needed  to  represent  a  full  binary  tree?     ●  Could  use  Tree  and  maintain  the  “fullness”  invariant   at  run)me.  Error  prone.   ●  Or  use  the  right  type  that  checks  this  at  compile   )me...    
  • 34. Full  Binary  Tree  (non-­‐regular  type!)   data Node i a = Node i a a data Full i a = Last a | Level (Full i (Node i a))   a  |  Node i a  |  Node i (Node i a)  |  ...   a  |  (i, a, a)  |  (i, (i, a, a), (i, a, a))  |  ...  
  • 35. Free  Monad     data Free f a = Pure a | Roll (f (Free f a)) Equivalent  to  this:  a | f (a | f ( a | … )) Not  this:  a | f a | f (f a) | ... data Node i a = Node i a a Free (Node i) a  is  equivalent  to  Tree i a, not Full i a
  • 36. More  Free  Monads   data Command a = WriteLine String a | ReadLine (String -> a) Free Command a = a | Command (a | Command (a | Command (...))) Represents  computa)ons  doing  specific  IO  opera)ons  in  a  type-­‐safe   manner!  
  • 37. Patrick  Prémont   Func)onal  Programming  Architect   BoldRadius  Solu)ons   @ppremont   www.boldradius.com