SlideShare a Scribd company logo
1 of 12
Download to read offline
8 MINUTES OF
FUNCTIONAL
PRIMITIVES
THINGS THAT YOU ARE AFRAID TO
ASK HASKELL DEVELOPERS
...is a control mechanism for sequencing computations.
MONAD
8MINUTESOFFUNCTIONALPRIMITIVES
...is a control mechanism for sequencing computations.
ExecutorService es =
Executors.newSingleThreadExecutor();
es.submit(() -> System.out.println("Message 1"));
es.submit(() -> System.out.println("Message 2"));
es.submit(() -> System.out.println("Message 3"));
es.shutdown();
MONAD
8MINUTESOFFUNCTIONALPRIMITIVES
because we can
We changing your state
Imperative
...is a control mechanism for sequencing computations.
Future(println(“Message 1”))
       .map(_ => println(“Message 2”))
       .map(_ => println(“Message 3”))
ExecutorService es =
Executors.newSingleThreadExecutor();
es.submit(() -> System.out.println("Message 1"));
es.submit(() -> System.out.println("Message 2"));
es.submit(() -> System.out.println("Message 3"));
es.shutdown();
MONAD
8MINUTESOFFUNCTIONALPRIMITIVES
because we can
We changing your state
Imperative
Functional
approach
WELCOME TO
...is a control mechanism for sequencing computations.
trait MyLittleClassyMonad { // lets call it just F[_]
}
MONAD
8MINUTESOFFUNCTIONALPRIMITIVES
...is a control mechanism for sequencing computations.
Value pure
trait MyLittleClassyMonad { // lets call it just F[_]
  def pure[A](value: A): F[A]
}
MONAD
8MINUTESOFFUNCTIONALPRIMITIVES
Value
...is a control mechanism for sequencing computations.
trait MyLittleClassyMonad { // lets call it just F[_]
  def pure[A](value: A): F[A]
  def flatMap[A, B](value: F[A])(f: A => F[B]): F[B]
}
MONAD
8MINUTESOFFUNCTIONALPRIMITIVES
flatMapValue A Value B
...have some rules!
LEFT IDENTITY: 
  a.pure.flatMap(f) == f(a)
RIGHT IDENTITY:
  m.flatMap(pure) == m
ASSOCIATIVITY:
  m.flatMap(f).flatMap(g) == m.flatMap(x => f(x).flatMap(g))
MONAD
8MINUTESOFFUNCTIONALPRIMITIVES
...every monad is a functor
trait FancyLittleFunctor {
   def map[A, B](value: F[A])(f: A => B): F[B]
}
FUNCTOR
8MINUTESOFFUNCTIONALPRIMITIVES
...every monad is a functor
trait FancyLittleFunctor {
   def map[A, B](value: F[A])(f: A => B): F[B]
}
trait FancyLittleFunctor extends MyLittleClassyMonad {
   def map[A, B](value: F[A])(f: A => B): F[B] =
                                     flatMap(value)(v => pure(f(v)))
}
FUNCTOR
8MINUTESOFFUNCTIONALPRIMITIVES
...when fail fast is not your case.
trait GorgeousCartesian {
   def product[A, B](fa: F[A], fb: F[B]): F[(A, B)]
}
CARTESIANS
8MINUTESOFFUNCTIONALPRIMITIVES
Value A Value B
Values
A and B
product
THANKS!
TIME TO ARGUE

More Related Content

Similar to 8 minutes of functional primitives

Template Poster A1 Portrait
Template Poster A1 PortraitTemplate Poster A1 Portrait
Template Poster A1 Portrait
Raybaen
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 Ds
Qundeel
 
Data Structure
Data StructureData Structure
Data Structure
sheraz1
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 Ds
Qundeel
 
Assignment 02 Process State SimulationCSci 430 Introduction to.docx
Assignment 02 Process State SimulationCSci 430 Introduction to.docxAssignment 02 Process State SimulationCSci 430 Introduction to.docx
Assignment 02 Process State SimulationCSci 430 Introduction to.docx
cargillfilberto
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OS
C.U
 
matlab-130408153714-phpapp02_lab123.ppsx
matlab-130408153714-phpapp02_lab123.ppsxmatlab-130408153714-phpapp02_lab123.ppsx
matlab-130408153714-phpapp02_lab123.ppsx
lekhacce
 

Similar to 8 minutes of functional primitives (20)

QConSF 2014 talk on Netflix Mantis, a stream processing system
QConSF 2014 talk on Netflix Mantis, a stream processing systemQConSF 2014 talk on Netflix Mantis, a stream processing system
QConSF 2014 talk on Netflix Mantis, a stream processing system
 
MODELLING, ANALYSIS AND SIMULATION OF DYNAMIC SYSTEMS USING CONTROL TECHNIQUE...
MODELLING, ANALYSIS AND SIMULATION OF DYNAMIC SYSTEMS USING CONTROL TECHNIQUE...MODELLING, ANALYSIS AND SIMULATION OF DYNAMIC SYSTEMS USING CONTROL TECHNIQUE...
MODELLING, ANALYSIS AND SIMULATION OF DYNAMIC SYSTEMS USING CONTROL TECHNIQUE...
 
Monitoring Complex Systems: Keeping Your Head on Straight in a Hard World
Monitoring Complex Systems: Keeping Your Head on Straight in a Hard WorldMonitoring Complex Systems: Keeping Your Head on Straight in a Hard World
Monitoring Complex Systems: Keeping Your Head on Straight in a Hard World
 
Computer-Aided Control Systems Design
Computer-Aided Control Systems DesignComputer-Aided Control Systems Design
Computer-Aided Control Systems Design
 
Mantis: Netflix's Event Stream Processing System
Mantis: Netflix's Event Stream Processing SystemMantis: Netflix's Event Stream Processing System
Mantis: Netflix's Event Stream Processing System
 
Munit junit test case
Munit junit test caseMunit junit test case
Munit junit test case
 
Template Poster A1 Portrait
Template Poster A1 PortraitTemplate Poster A1 Portrait
Template Poster A1 Portrait
 
REAL TIME OPERATING SYSTEM PART 1
REAL TIME OPERATING SYSTEM PART 1REAL TIME OPERATING SYSTEM PART 1
REAL TIME OPERATING SYSTEM PART 1
 
Junit in mule demo
Junit in mule demoJunit in mule demo
Junit in mule demo
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 Ds
 
Data Structure
Data StructureData Structure
Data Structure
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 Ds
 
Assignment 02 Process State SimulationCSci 430 Introduction to.docx
Assignment 02 Process State SimulationCSci 430 Introduction to.docxAssignment 02 Process State SimulationCSci 430 Introduction to.docx
Assignment 02 Process State SimulationCSci 430 Introduction to.docx
 
Art of unit testing: How developer should care about code quality
Art of unit testing: How developer should care about code qualityArt of unit testing: How developer should care about code quality
Art of unit testing: How developer should care about code quality
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OS
 
Celery with python
Celery with pythonCelery with python
Celery with python
 
matlab-130408153714-phpapp02_lab123.ppsx
matlab-130408153714-phpapp02_lab123.ppsxmatlab-130408153714-phpapp02_lab123.ppsx
matlab-130408153714-phpapp02_lab123.ppsx
 
Tues 115pm cassandra + s3 + hadoop = quick auditing and analytics_yazovskiy
Tues 115pm cassandra + s3 + hadoop = quick auditing and analytics_yazovskiyTues 115pm cassandra + s3 + hadoop = quick auditing and analytics_yazovskiy
Tues 115pm cassandra + s3 + hadoop = quick auditing and analytics_yazovskiy
 
Polyglot Persistence in the Real World: Cassandra + S3 + MapReduce
Polyglot Persistence in the Real World: Cassandra + S3 + MapReducePolyglot Persistence in the Real World: Cassandra + S3 + MapReduce
Polyglot Persistence in the Real World: Cassandra + S3 + MapReduce
 
Matlab basic and image
Matlab basic and imageMatlab basic and image
Matlab basic and image
 

Recently uploaded

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
HenryBriggs2
 
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
Health
 

Recently uploaded (20)

Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 

8 minutes of functional primitives

  • 1. 8 MINUTES OF FUNCTIONAL PRIMITIVES THINGS THAT YOU ARE AFRAID TO ASK HASKELL DEVELOPERS
  • 2. ...is a control mechanism for sequencing computations. MONAD 8MINUTESOFFUNCTIONALPRIMITIVES
  • 3. ...is a control mechanism for sequencing computations. ExecutorService es = Executors.newSingleThreadExecutor(); es.submit(() -> System.out.println("Message 1")); es.submit(() -> System.out.println("Message 2")); es.submit(() -> System.out.println("Message 3")); es.shutdown(); MONAD 8MINUTESOFFUNCTIONALPRIMITIVES because we can We changing your state Imperative
  • 4. ...is a control mechanism for sequencing computations. Future(println(“Message 1”))        .map(_ => println(“Message 2”))        .map(_ => println(“Message 3”)) ExecutorService es = Executors.newSingleThreadExecutor(); es.submit(() -> System.out.println("Message 1")); es.submit(() -> System.out.println("Message 2")); es.submit(() -> System.out.println("Message 3")); es.shutdown(); MONAD 8MINUTESOFFUNCTIONALPRIMITIVES because we can We changing your state Imperative Functional approach WELCOME TO
  • 5. ...is a control mechanism for sequencing computations. trait MyLittleClassyMonad { // lets call it just F[_] } MONAD 8MINUTESOFFUNCTIONALPRIMITIVES
  • 6. ...is a control mechanism for sequencing computations. Value pure trait MyLittleClassyMonad { // lets call it just F[_]   def pure[A](value: A): F[A] } MONAD 8MINUTESOFFUNCTIONALPRIMITIVES Value
  • 7. ...is a control mechanism for sequencing computations. trait MyLittleClassyMonad { // lets call it just F[_]   def pure[A](value: A): F[A]   def flatMap[A, B](value: F[A])(f: A => F[B]): F[B] } MONAD 8MINUTESOFFUNCTIONALPRIMITIVES flatMapValue A Value B
  • 8. ...have some rules! LEFT IDENTITY:    a.pure.flatMap(f) == f(a) RIGHT IDENTITY:   m.flatMap(pure) == m ASSOCIATIVITY:   m.flatMap(f).flatMap(g) == m.flatMap(x => f(x).flatMap(g)) MONAD 8MINUTESOFFUNCTIONALPRIMITIVES
  • 9. ...every monad is a functor trait FancyLittleFunctor {    def map[A, B](value: F[A])(f: A => B): F[B] } FUNCTOR 8MINUTESOFFUNCTIONALPRIMITIVES
  • 10. ...every monad is a functor trait FancyLittleFunctor {    def map[A, B](value: F[A])(f: A => B): F[B] } trait FancyLittleFunctor extends MyLittleClassyMonad {    def map[A, B](value: F[A])(f: A => B): F[B] =                                      flatMap(value)(v => pure(f(v))) } FUNCTOR 8MINUTESOFFUNCTIONALPRIMITIVES
  • 11. ...when fail fast is not your case. trait GorgeousCartesian {    def product[A, B](fa: F[A], fb: F[B]): F[(A, B)] } CARTESIANS 8MINUTESOFFUNCTIONALPRIMITIVES Value A Value B Values A and B product