SlideShare a Scribd company logo
1 of 21
Download to read offline
ELI5: What the heck is a
Functor?
Imperative programming
• Serial execution of sequence of steps
• A global state of the program exists
• Each step may change the program state
// Javascript
1 function divBy(x) {
2 return 10 / x;
3 }
4
5 function triple(x) {
6 return x * 3;
7 }
8
9 function run(x) {
10 var a = divBy(x);
11 var b = triple(a);
12 return b;
13 }
Functional Programming
• Executed by evaluating expressions
• Typically Immutable
• Functions are first class
• Higher order functions
• FP focusses on the what rather than the how
-- Haskell
1
2 divBy x = 10 / x
3
4 triple x = x * 3
5
6 run x = triple (divBy x)
Types
What is a type?
• A type is a property or a label
• Every expression has a type
• Types exist in a dynamic or static language
• Only understood by the compiler
var name = "Earnest";
console.log(typeof(name)); // => "string"
var age = 24;
console.log(typeof(age)); // "number"
Dynamic Typing
Types we use everyday
• Integer, Double
• Boolean
• Arrays
• Objects (dictionary or maps in some dynamic languages)
name :: String
name = "Earnest" -- :type name => String
age :: Integer
age = 42 -- :type age => Integer
Static Typing
Live Exercise: Build
custom types
Functions can have
types too!
// Javascript
1 function divBy(x) {
2 return 10 / x;
3 }
4
5 function triple(x) {
6 return x * 3;
7 }
8
9 function run(x) {
10 var a = divBy(x);
11 var b = triple(a);
12 return b;
13 }
Enforced by you
run(10)
=> 3
run(“10”)
=> 3
run(“hello”)
=> NaN
Enforced by the language
1 -- Haskell
2 divBy :: Double -> Double
3 divBy x = 10 / 2
4
5 triple :: Double -> Double
6 triple x = x * 3
7
8 run :: Double -> Double
9 run x = triple (divBy x)
run(10)
=> 3.0
=> <interactive>:8:5: error:
- Couldn't match expected type 'Double' with
actual type 'String'
- In the first argument of 'run', namely '("10"
)'
In the expression: run ("10")
In an equation for 'it': it = run ("10")
run("10")
Errors & Exceptions
"I call it my billion-dollar mistake. It was the invention
of the null reference in 1965."
– Tony Hoare, inventor of ALGOL W.
• Null pointer exception
• Divide by zero
Can the type system help
us?
Live Exercise 2:
Implement error handling
What is a functor?
A functor is a way to apply a function over or around
some structure that we don’t want to alter
class Functor f where

fmap :: (a -> b) -> f a -> f b
Live Exercise 3:
Implement Functor
Thanks!
Questions?

More Related Content

What's hot

Hipster Oriented Programming
Hipster Oriented ProgrammingHipster Oriented Programming
Hipster Oriented Programming
Jens Ravens
 
What is python
What is pythonWhat is python
What is python
EU Edge
 
An Introduction to Functional Programming with Javascript
An Introduction to Functional Programming with JavascriptAn Introduction to Functional Programming with Javascript
An Introduction to Functional Programming with Javascript
Doug Sparling
 

What's hot (19)

Slide
SlideSlide
Slide
 
Introducing Ruby
Introducing RubyIntroducing Ruby
Introducing Ruby
 
this is simple
this is simplethis is simple
this is simple
 
C++ quik notes
C++ quik notesC++ quik notes
C++ quik notes
 
Hipster Oriented Programming
Hipster Oriented ProgrammingHipster Oriented Programming
Hipster Oriented Programming
 
Os Welton
Os WeltonOs Welton
Os Welton
 
What is python
What is pythonWhat is python
What is python
 
Learn Ruby 2011 - Session 5 - Looking for a Rescue
Learn Ruby 2011 - Session 5 - Looking for a RescueLearn Ruby 2011 - Session 5 - Looking for a Rescue
Learn Ruby 2011 - Session 5 - Looking for a Rescue
 
Unsafe to typesafe
Unsafe to typesafeUnsafe to typesafe
Unsafe to typesafe
 
An Introduction to Functional Programming with Javascript
An Introduction to Functional Programming with JavascriptAn Introduction to Functional Programming with Javascript
An Introduction to Functional Programming with Javascript
 
Ruby Exceptions
Ruby ExceptionsRuby Exceptions
Ruby Exceptions
 
Typescript tips & tricks
Typescript tips & tricksTypescript tips & tricks
Typescript tips & tricks
 
Hipster oriented programming (Mobilization Lodz 2015)
Hipster oriented programming (Mobilization Lodz 2015)Hipster oriented programming (Mobilization Lodz 2015)
Hipster oriented programming (Mobilization Lodz 2015)
 
Php Chapter 2 3 Training
Php Chapter 2 3 TrainingPhp Chapter 2 3 Training
Php Chapter 2 3 Training
 
Javascript status 2016
Javascript status 2016Javascript status 2016
Javascript status 2016
 
PHP 7.0 new features (and new interpreter)
PHP 7.0 new features (and new interpreter)PHP 7.0 new features (and new interpreter)
PHP 7.0 new features (and new interpreter)
 
Javascript good parts - for novice programmers
Javascript good parts - for novice programmersJavascript good parts - for novice programmers
Javascript good parts - for novice programmers
 
Donetconf2016: The Future of C#
Donetconf2016: The Future of C#Donetconf2016: The Future of C#
Donetconf2016: The Future of C#
 
Frege - consequently functional programming for the JVM
Frege - consequently functional programming for the JVMFrege - consequently functional programming for the JVM
Frege - consequently functional programming for the JVM
 

Similar to ELI5: What the heck is a Functor?

Clojure - A new Lisp
Clojure - A new LispClojure - A new Lisp
Clojure - A new Lisp
elliando dias
 
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code styleRuby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
Anton Shemerey
 
Java script questions
Java script questionsJava script questions
Java script questions
Srikanth
 

Similar to ELI5: What the heck is a Functor? (20)

Clojure - A new Lisp
Clojure - A new LispClojure - A new Lisp
Clojure - A new Lisp
 
Clojure class
Clojure classClojure class
Clojure class
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
 
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code styleRuby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
 
Java script questions
Java script questionsJava script questions
Java script questions
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
 
Groovy presentation
Groovy presentationGroovy presentation
Groovy presentation
 
(map Clojure everyday-tasks)
(map Clojure everyday-tasks)(map Clojure everyday-tasks)
(map Clojure everyday-tasks)
 
Php + my sql
Php + my sqlPhp + my sql
Php + my sql
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
Clojure
ClojureClojure
Clojure
 
Exploring Clojurescript
Exploring ClojurescriptExploring Clojurescript
Exploring Clojurescript
 
ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015
 
Ruby is an Acceptable Lisp
Ruby is an Acceptable LispRuby is an Acceptable Lisp
Ruby is an Acceptable Lisp
 
Drupaljam xl 2019 presentation multilingualism makes better programmers
Drupaljam xl 2019 presentation   multilingualism makes better programmersDrupaljam xl 2019 presentation   multilingualism makes better programmers
Drupaljam xl 2019 presentation multilingualism makes better programmers
 
Rakudo
RakudoRakudo
Rakudo
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
Groovy Ecosystem - JFokus 2011 - Guillaume Laforge
Groovy Ecosystem - JFokus 2011 - Guillaume LaforgeGroovy Ecosystem - JFokus 2011 - Guillaume Laforge
Groovy Ecosystem - JFokus 2011 - Guillaume Laforge
 
Java script for web developer
Java script for web developerJava script for web developer
Java script for web developer
 
Perl Intro 4 Debugger
Perl Intro 4 DebuggerPerl Intro 4 Debugger
Perl Intro 4 Debugger
 

Recently uploaded

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Recently uploaded (20)

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 

ELI5: What the heck is a Functor?

  • 1. ELI5: What the heck is a Functor?
  • 2. Imperative programming • Serial execution of sequence of steps • A global state of the program exists • Each step may change the program state
  • 3. // Javascript 1 function divBy(x) { 2 return 10 / x; 3 } 4 5 function triple(x) { 6 return x * 3; 7 } 8 9 function run(x) { 10 var a = divBy(x); 11 var b = triple(a); 12 return b; 13 }
  • 4. Functional Programming • Executed by evaluating expressions • Typically Immutable • Functions are first class • Higher order functions • FP focusses on the what rather than the how
  • 5. -- Haskell 1 2 divBy x = 10 / x 3 4 triple x = x * 3 5 6 run x = triple (divBy x)
  • 7. What is a type? • A type is a property or a label • Every expression has a type • Types exist in a dynamic or static language • Only understood by the compiler
  • 8. var name = "Earnest"; console.log(typeof(name)); // => "string" var age = 24; console.log(typeof(age)); // "number" Dynamic Typing
  • 9. Types we use everyday • Integer, Double • Boolean • Arrays • Objects (dictionary or maps in some dynamic languages)
  • 10. name :: String name = "Earnest" -- :type name => String age :: Integer age = 42 -- :type age => Integer Static Typing
  • 13. // Javascript 1 function divBy(x) { 2 return 10 / x; 3 } 4 5 function triple(x) { 6 return x * 3; 7 } 8 9 function run(x) { 10 var a = divBy(x); 11 var b = triple(a); 12 return b; 13 } Enforced by you run(10) => 3 run(“10”) => 3 run(“hello”) => NaN
  • 14. Enforced by the language 1 -- Haskell 2 divBy :: Double -> Double 3 divBy x = 10 / 2 4 5 triple :: Double -> Double 6 triple x = x * 3 7 8 run :: Double -> Double 9 run x = triple (divBy x) run(10) => 3.0 => <interactive>:8:5: error: - Couldn't match expected type 'Double' with actual type 'String' - In the first argument of 'run', namely '("10" )' In the expression: run ("10") In an equation for 'it': it = run ("10") run("10")
  • 15. Errors & Exceptions "I call it my billion-dollar mistake. It was the invention of the null reference in 1965." – Tony Hoare, inventor of ALGOL W. • Null pointer exception • Divide by zero
  • 16. Can the type system help us?
  • 17. Live Exercise 2: Implement error handling
  • 18. What is a functor? A functor is a way to apply a function over or around some structure that we don’t want to alter
  • 19. class Functor f where
 fmap :: (a -> b) -> f a -> f b