SlideShare a Scribd company logo
1 of 41
Download to read offline
DEMYSTIFY FUNCTIONAL
PROGRAMMING IN
(OR TRYING TO … 🤔)
ENNIO MASI
SENIOR IOS DEV @ YOOX NET-A-PORTER
IN COMPUTER SCIENCE, FP IS A PROGRAMMING
PARADIGM - A STYLE OF BUILDING THE STRUCTURE AND
ELEMENTS OF COMPUTER PROGRAMS - THAT TREATS
COMPUTATION AS THE EVALUATION OF MATHEMATICAL
FUNCTIONS AND AVOIDS CHANGING - STATE AND
MUTABLE DATA.
IN COMPUTER SCIENCE, FP IS A PROGRAMMING
PARADIGM - A STYLE OF BUILDING THE STRUCTURE AND
ELEMENTS OF COMPUTER PROGRAMS - THAT TREATS
COMPUTATION AS THE EVALUATION OF MATHEMATICAL
FUNCTIONS AND AVOIDS CHANGING - STATE AND
MUTABLE DATA.
FUNCTIONAL PROGRAMMING
IN COMPUTER SCIENCE, FP IS A PROGRAMMING
PARADIGM - A STYLE OF BUILDING THE STRUCTURE AND
ELEMENTS OF COMPUTER PROGRAMS - THAT TREATS
COMPUTATION AS THE EVALUATION OF MATHEMATICAL
FUNCTIONS AND AVOIDS CHANGING - STATE AND
MUTABLE DATA.
IN COMPUTER SCIENCE, FP IS A PROGRAMMING
PARADIGM - A STYLE OF BUILDING THE STRUCTURE AND
ELEMENTS OF COMPUTER PROGRAMS - THAT TREATS
COMPUTATION AS THE EVALUATION OF MATHEMATICAL
FUNCTIONS AND AVOIDS CHANGING - STATE AND
MUTABLE DATA.
Someone on Wikipedia
FP HAS ITS ORIGINS IN LAMBDA CALCULUS, A FORMAL SYSTEM
DEVELOPED IN THE 1930S TO INVESTIGATE COMPUTABILITY, THE
ENTSCHEIDUNGSPROBLEM, FUNCTION DEFINITION, FUNCTION
APPLICATION, AND RECURSION. MANY FUNCTIONAL
PROGRAMMING LANGUAGES CAN BE VIEWED AS ELABORATIONS
ON THE LAMBDA CALCULUS.
Someone else on Wikipedia
FUNCTIONAL PROGRAMMING
FP HAS ITS ORIGINS IN LAMBDA CALCULUS, A FORMAL SYSTEM
DEVELOPED IN THE 1930S TO INVESTIGATE COMPUTABILITY, THE
ENTSCHEIDUNGSPROBLEM, FUNCTION DEFINITION, FUNCTION
APPLICATION, AND RECURSION. MANY FUNCTIONAL
PROGRAMMING LANGUAGES CAN BE VIEWED AS ELABORATIONS
ON THE LAMBDA CALCULUS.
FP HAS ITS ORIGINS IN LAMBDA CALCULUS, A FORMAL SYSTEM
DEVELOPED IN THE 1930S TO INVESTIGATE COMPUTABILITY, THE
ENTSCHEIDUNGSPROBLEM, FUNCTION DEFINITION, FUNCTION
APPLICATION, AND RECURSION. MANY FUNCTIONAL
PROGRAMMING LANGUAGES CAN BE VIEWED AS ELABORATIONS
ON THE LAMBDA CALCULUS.
FP HAS ITS ORIGINS IN LAMBDA CALCULUS, A FORMAL SYSTEM
DEVELOPED IN THE 1930S TO INVESTIGATE COMPUTABILITY, THE
ENTSCHEIDUNGSPROBLEM, FUNCTION DEFINITION, FUNCTION
APPLICATION, AND RECURSION. MANY FUNCTIONAL
PROGRAMMING LANGUAGES CAN BE VIEWED AS ELABORATIONS
ON THE LAMBDA CALCULUS.
FOUNDATIONS
FP IS BASED ON CATEGORY THEORY WHICH IS REALLY COMPLEX
FUNCTIONAL PROGRAMMING (HATERS)
FUNCTIONAL PROGRAMMING (LOVERS)
FOUNDATIONS
START
FOUNDATIONS
FP IS AN APPROACH BASED ON FUNCTION CALLS
F(X) —> Y
FOUNDATIONS
FP IS AN APPROACH BASED ON FUNCTION CALLS
OOP FP
Single
Responsability
functions
Open Closed
Principle
functions
Dependecy
Inversion
functions
Factory pattern functions
… …
FOUNDATIONS
F(X) = Y
▸ Write predictable functions
FP IS AN APPROACH BASED ON FUNCTION CALLS
▸ Code easier to test
▸ Thread-safe codebase
▸ Features chaining
FOUNDATIONS
FP IS AN APPROACH BASED ON FUNCTION CALLS
FP IS AN APPROACH BASED ON PURE FUNCTION CALLS
FOUNDATIONS
F(X) = Y
FP IS AN APPROACH BASED ON PURE FUNCTION CALLS
currentDate
20/11/2018
INPUT
tellMeTheTruth(..) ?
OUTPUT
FOUNDATIONS
F(X) = Y
FP IS AN APPROACH BASED ON PURE FUNCTION CALLS
currentDate
20/11/2018
INPUT
tellMeTheTruth(..) false
OUTPUT
current date
21/11/2018
FOUNDATIONS
F(X) = Y
FP IS AN APPROACH BASED ON PURE FUNCTION CALLS
currentDate
20/11/2018
INPUT
migrationStatus() true
OUTPUT
current date
19/11/2018
FOUNDATIONS
F(X) = Y
FP IS AN APPROACH BASED ON PURE FUNCTION CALLS
NOT PURE
NO SIDE EFFECTS, TESTABLE, THREAD SAFE
PURE
FOUNDATIONS
F(X) = Y
FP IS AN APPROACH BASED ON PURE FUNCTION CALLS
NOT PURE
NO SIDE EFFECTS, TESTABLE, THREAD SAFE
PURE
FOUNDATIONS
F(X) = Y
FP IS AN APPROACH BASED ON PURE FUNCTION CALLS
NOT PURE
NO SIDE EFFECTS, TESTABLE, THREAD SAFE
PURE
FOUNDATIONS
FP IS AN APPROACH BASED ON FUNCTION CALLS
FP IS AN APPROACH BASED ON PURE FUNCTION CALLS
FP IS AN APPROACH BASED ON COMPOSITION OF PURE FUNCTION CALLS
FOUNDATIONS
FP IS AN APPROACH BASED ON COMPOSITION OF PURE FUNCTION CALLS
f(2) —> 2 * 3 = 6 g(6) —> 6 * 6 = 36
G(F(A)) = B
g(f(2)) ➡ f(2) = 6 ➡ g(6) = 36
FOUNDATIONS
FP IS AN APPROACH BASED ON COMPOSITION OF PURE FUNCTION CALLS
FUNCTIONS ARE FIRST CLASS CITIZENS
HIGH ORDER FUNCTIONS
SWIFT SUPPORTS FP
FOUNDATIONS
FP IS AN APPROACH BASED ON COMPOSITION OF PURE FUNCTION CALLS
COMPOSITION
FOUNDATIONS
FP IS AN APPROACH BASED ON COMPOSITION OF PURE FUNCTION CALLS
F: A —>B G: B —>C
F: INT —>INT G: INT —>INT
F: INT —>INT G: INT —>INT H: INT —>STR
FOUNDATIONS
FP IS AN APPROACH BASED ON COMPOSITION OF PURE FUNCTION CALLS
FOUNDATIONS
FP IS AN APPROACH BASED ON COMPOSITION OF PURE FUNCTION CALLS
▸ Write predictable functions
▸ Code easier to test
▸ Thread-safe codebase
▸ Features chaining
FOUNDATIONS: IMMUTABILITY
FOUNDATIONS: IMMUTABILITY
▸ Thread-safe
ADVANTAGES OF IMMUTABILITY
▸ Chaining of functions
FOUNDATIONS: IMMUTABILITY
OOP (IMPERATIVE) VS FP
S + DATA[I] IS “ILLEGAL” IN FP 😈
PURE FUNCTIONS MUST BE STATELESS
FOUNDATIONS: IMMUTABILITY
OOP (IMPERATIVE) VS FP
TAIL RECURSION IS NOT OPTIMISED IN SWIFT 🙈
TAIL RECURSION
SWIFT PARTIALLY SUPPORTS FP
“A monad is just a monoid in the category of
endofunctors”
Someone on Internet
FOUNDATIONS: FUNCTORS & MONADS
FOUNDATIONS: FUNCTORS & MONADS
SWIFT OPTIONAL
OPTIONAL IS A “CONTEXT” THAT CAN INCLUDE A VALUE OR NOT
FOUNDATIONS: FUNCTORS & MONADS
FUNCTORS
▸ SINCE OPTIONAL IS A “CONTEXT”, WE NEED A WAY TO EXTRACT THE POSSIBLE
VALUE FROM IT
▸ MAP 😍
FOUNDATIONS: FUNCTORS & MONADS
FUNCTORS
▸ SINCE OPTIONAL IS A “CONTEXT”, WE NEED A WAY TO EXTRACT THE POSSIBLE
VALUE FROM IT
▸ MAP 😍
OPTIONAL MAP VALUE
FOUNDATIONS: FUNCTORS & MONADS
FUNCTORS
OPTIONAL MAP VALUE
A FUNCTOR IS A TYPE THAT CONTAINS A VALUE AND PROVIDES A MAP FUNCTION AS INTERFACE
FUNCTOR MAP VALUE
HINT: OPTIONAL IS A FUNCTOR
FP
FOUNDATIONS: FUNCTORS & MONADS
FUNCTORS
A FUNCTOR IS A TYPE THAT CONTAINS A VALUE AND PROVIDES A MAP FUNCTION AS INTERFACE
HINT: OPTIONAL IS A FUNCTOR
▸ Identity Law
▸ Composition Law
FOUNDATIONS: FUNCTORS & MONADS
MONADS
A MONAD IS JUST A MONOID IN THE BLA BLA BLA …
▸ Well a monad is just a type on which we can apply flatMap (bind)
FEATURES CHAINING (ON VALUES)
HINT: OPTIONAL IS A MONAD &
FOUNDATIONS: FEATURES CHAINING
MONADS IN PRACTICE
Well a monad is just a type on which we can apply flatMap (bind)
FOUNDATIONS: FEATURES CHAINING
MONADS
FOUNDATIONS: FEATURES CHAINING
string
f1 f2 f Out Monad
String bool bool bool
error error error error
bind return return
FOUNDATIONS: FEATURES CHAINING
string
f1 f2 f Out Monad
bool bool bool bool
error error error error
bind return return
FEATURES CHAINING (ON VALUES)
ERROR MANAGEMENT
VS
PLAYGROUND TIME 🔥
https://github.com/ennioma/FPTalk-Playground

More Related Content

Recently uploaded

AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 

Recently uploaded (20)

OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Demystify Functional Programming in Swift

  • 1. DEMYSTIFY FUNCTIONAL PROGRAMMING IN (OR TRYING TO … 🤔) ENNIO MASI SENIOR IOS DEV @ YOOX NET-A-PORTER
  • 2. IN COMPUTER SCIENCE, FP IS A PROGRAMMING PARADIGM - A STYLE OF BUILDING THE STRUCTURE AND ELEMENTS OF COMPUTER PROGRAMS - THAT TREATS COMPUTATION AS THE EVALUATION OF MATHEMATICAL FUNCTIONS AND AVOIDS CHANGING - STATE AND MUTABLE DATA. IN COMPUTER SCIENCE, FP IS A PROGRAMMING PARADIGM - A STYLE OF BUILDING THE STRUCTURE AND ELEMENTS OF COMPUTER PROGRAMS - THAT TREATS COMPUTATION AS THE EVALUATION OF MATHEMATICAL FUNCTIONS AND AVOIDS CHANGING - STATE AND MUTABLE DATA. FUNCTIONAL PROGRAMMING IN COMPUTER SCIENCE, FP IS A PROGRAMMING PARADIGM - A STYLE OF BUILDING THE STRUCTURE AND ELEMENTS OF COMPUTER PROGRAMS - THAT TREATS COMPUTATION AS THE EVALUATION OF MATHEMATICAL FUNCTIONS AND AVOIDS CHANGING - STATE AND MUTABLE DATA. IN COMPUTER SCIENCE, FP IS A PROGRAMMING PARADIGM - A STYLE OF BUILDING THE STRUCTURE AND ELEMENTS OF COMPUTER PROGRAMS - THAT TREATS COMPUTATION AS THE EVALUATION OF MATHEMATICAL FUNCTIONS AND AVOIDS CHANGING - STATE AND MUTABLE DATA. Someone on Wikipedia
  • 3. FP HAS ITS ORIGINS IN LAMBDA CALCULUS, A FORMAL SYSTEM DEVELOPED IN THE 1930S TO INVESTIGATE COMPUTABILITY, THE ENTSCHEIDUNGSPROBLEM, FUNCTION DEFINITION, FUNCTION APPLICATION, AND RECURSION. MANY FUNCTIONAL PROGRAMMING LANGUAGES CAN BE VIEWED AS ELABORATIONS ON THE LAMBDA CALCULUS. Someone else on Wikipedia FUNCTIONAL PROGRAMMING FP HAS ITS ORIGINS IN LAMBDA CALCULUS, A FORMAL SYSTEM DEVELOPED IN THE 1930S TO INVESTIGATE COMPUTABILITY, THE ENTSCHEIDUNGSPROBLEM, FUNCTION DEFINITION, FUNCTION APPLICATION, AND RECURSION. MANY FUNCTIONAL PROGRAMMING LANGUAGES CAN BE VIEWED AS ELABORATIONS ON THE LAMBDA CALCULUS. FP HAS ITS ORIGINS IN LAMBDA CALCULUS, A FORMAL SYSTEM DEVELOPED IN THE 1930S TO INVESTIGATE COMPUTABILITY, THE ENTSCHEIDUNGSPROBLEM, FUNCTION DEFINITION, FUNCTION APPLICATION, AND RECURSION. MANY FUNCTIONAL PROGRAMMING LANGUAGES CAN BE VIEWED AS ELABORATIONS ON THE LAMBDA CALCULUS. FP HAS ITS ORIGINS IN LAMBDA CALCULUS, A FORMAL SYSTEM DEVELOPED IN THE 1930S TO INVESTIGATE COMPUTABILITY, THE ENTSCHEIDUNGSPROBLEM, FUNCTION DEFINITION, FUNCTION APPLICATION, AND RECURSION. MANY FUNCTIONAL PROGRAMMING LANGUAGES CAN BE VIEWED AS ELABORATIONS ON THE LAMBDA CALCULUS.
  • 4. FOUNDATIONS FP IS BASED ON CATEGORY THEORY WHICH IS REALLY COMPLEX
  • 8. FOUNDATIONS FP IS AN APPROACH BASED ON FUNCTION CALLS F(X) —> Y
  • 9. FOUNDATIONS FP IS AN APPROACH BASED ON FUNCTION CALLS OOP FP Single Responsability functions Open Closed Principle functions Dependecy Inversion functions Factory pattern functions … …
  • 10. FOUNDATIONS F(X) = Y ▸ Write predictable functions FP IS AN APPROACH BASED ON FUNCTION CALLS ▸ Code easier to test ▸ Thread-safe codebase ▸ Features chaining
  • 11. FOUNDATIONS FP IS AN APPROACH BASED ON FUNCTION CALLS FP IS AN APPROACH BASED ON PURE FUNCTION CALLS
  • 12. FOUNDATIONS F(X) = Y FP IS AN APPROACH BASED ON PURE FUNCTION CALLS currentDate 20/11/2018 INPUT tellMeTheTruth(..) ? OUTPUT
  • 13. FOUNDATIONS F(X) = Y FP IS AN APPROACH BASED ON PURE FUNCTION CALLS currentDate 20/11/2018 INPUT tellMeTheTruth(..) false OUTPUT current date 21/11/2018
  • 14. FOUNDATIONS F(X) = Y FP IS AN APPROACH BASED ON PURE FUNCTION CALLS currentDate 20/11/2018 INPUT migrationStatus() true OUTPUT current date 19/11/2018
  • 15. FOUNDATIONS F(X) = Y FP IS AN APPROACH BASED ON PURE FUNCTION CALLS NOT PURE NO SIDE EFFECTS, TESTABLE, THREAD SAFE PURE
  • 16. FOUNDATIONS F(X) = Y FP IS AN APPROACH BASED ON PURE FUNCTION CALLS NOT PURE NO SIDE EFFECTS, TESTABLE, THREAD SAFE PURE
  • 17. FOUNDATIONS F(X) = Y FP IS AN APPROACH BASED ON PURE FUNCTION CALLS NOT PURE NO SIDE EFFECTS, TESTABLE, THREAD SAFE PURE
  • 18. FOUNDATIONS FP IS AN APPROACH BASED ON FUNCTION CALLS FP IS AN APPROACH BASED ON PURE FUNCTION CALLS FP IS AN APPROACH BASED ON COMPOSITION OF PURE FUNCTION CALLS
  • 19. FOUNDATIONS FP IS AN APPROACH BASED ON COMPOSITION OF PURE FUNCTION CALLS f(2) —> 2 * 3 = 6 g(6) —> 6 * 6 = 36 G(F(A)) = B g(f(2)) ➡ f(2) = 6 ➡ g(6) = 36
  • 20. FOUNDATIONS FP IS AN APPROACH BASED ON COMPOSITION OF PURE FUNCTION CALLS FUNCTIONS ARE FIRST CLASS CITIZENS HIGH ORDER FUNCTIONS SWIFT SUPPORTS FP
  • 21. FOUNDATIONS FP IS AN APPROACH BASED ON COMPOSITION OF PURE FUNCTION CALLS COMPOSITION
  • 22. FOUNDATIONS FP IS AN APPROACH BASED ON COMPOSITION OF PURE FUNCTION CALLS F: A —>B G: B —>C F: INT —>INT G: INT —>INT F: INT —>INT G: INT —>INT H: INT —>STR
  • 23. FOUNDATIONS FP IS AN APPROACH BASED ON COMPOSITION OF PURE FUNCTION CALLS
  • 24. FOUNDATIONS FP IS AN APPROACH BASED ON COMPOSITION OF PURE FUNCTION CALLS ▸ Write predictable functions ▸ Code easier to test ▸ Thread-safe codebase ▸ Features chaining
  • 26. FOUNDATIONS: IMMUTABILITY ▸ Thread-safe ADVANTAGES OF IMMUTABILITY ▸ Chaining of functions
  • 27. FOUNDATIONS: IMMUTABILITY OOP (IMPERATIVE) VS FP S + DATA[I] IS “ILLEGAL” IN FP 😈 PURE FUNCTIONS MUST BE STATELESS
  • 28. FOUNDATIONS: IMMUTABILITY OOP (IMPERATIVE) VS FP TAIL RECURSION IS NOT OPTIMISED IN SWIFT 🙈 TAIL RECURSION SWIFT PARTIALLY SUPPORTS FP
  • 29. “A monad is just a monoid in the category of endofunctors” Someone on Internet FOUNDATIONS: FUNCTORS & MONADS
  • 30.
  • 31. FOUNDATIONS: FUNCTORS & MONADS SWIFT OPTIONAL OPTIONAL IS A “CONTEXT” THAT CAN INCLUDE A VALUE OR NOT
  • 32. FOUNDATIONS: FUNCTORS & MONADS FUNCTORS ▸ SINCE OPTIONAL IS A “CONTEXT”, WE NEED A WAY TO EXTRACT THE POSSIBLE VALUE FROM IT ▸ MAP 😍
  • 33. FOUNDATIONS: FUNCTORS & MONADS FUNCTORS ▸ SINCE OPTIONAL IS A “CONTEXT”, WE NEED A WAY TO EXTRACT THE POSSIBLE VALUE FROM IT ▸ MAP 😍 OPTIONAL MAP VALUE
  • 34. FOUNDATIONS: FUNCTORS & MONADS FUNCTORS OPTIONAL MAP VALUE A FUNCTOR IS A TYPE THAT CONTAINS A VALUE AND PROVIDES A MAP FUNCTION AS INTERFACE FUNCTOR MAP VALUE HINT: OPTIONAL IS A FUNCTOR FP
  • 35. FOUNDATIONS: FUNCTORS & MONADS FUNCTORS A FUNCTOR IS A TYPE THAT CONTAINS A VALUE AND PROVIDES A MAP FUNCTION AS INTERFACE HINT: OPTIONAL IS A FUNCTOR ▸ Identity Law ▸ Composition Law
  • 36. FOUNDATIONS: FUNCTORS & MONADS MONADS A MONAD IS JUST A MONOID IN THE BLA BLA BLA … ▸ Well a monad is just a type on which we can apply flatMap (bind) FEATURES CHAINING (ON VALUES) HINT: OPTIONAL IS A MONAD &
  • 37. FOUNDATIONS: FEATURES CHAINING MONADS IN PRACTICE Well a monad is just a type on which we can apply flatMap (bind)
  • 39. FOUNDATIONS: FEATURES CHAINING string f1 f2 f Out Monad String bool bool bool error error error error bind return return
  • 40. FOUNDATIONS: FEATURES CHAINING string f1 f2 f Out Monad bool bool bool bool error error error error bind return return FEATURES CHAINING (ON VALUES) ERROR MANAGEMENT VS