SlideShare a Scribd company logo
1 of 86
@pcameronpresley@pcameronpresley
LEARNING FP THROUGH CONSTRUCTION:
FIRST PRINCIPLES
Cameron Presley
@pcameronpresley
Cameron@TheSoftwareMentor.com
Slides can be found at:
https://blog.TheSoftwareMentor.com/Presentations
@pcameronpresley
@pcameronpresley@pcameronpresley
Hello!
@pcameronpresley@pcameronpresley
Outline
▪ Introduction to Mars Rover Kata
▪ Determine data models
▪ Determine functions
▪ Wire everything together
@pcameronpresley@pcameronpresley 5
@pcameronpresley@pcameronpresley
Objective
You are part of the team that explores Mars by
sending remotely controlled vehicles to the
surface of the planet.
Develop an API that translates the commands
sent from earth to instructions that are
understood by the rover.
@pcameronpresley@pcameronpresley
Requirements
▪ Know what the initial starting point (x,y) of a
rover and the direction (N,S,E,W) it is facing.
▪ Implement commands that move the rover
forward/backward (f,b).
▪ Implement commands that turn the rover
left/right (l,r).
@pcameronpresley@pcameronpresley
Requirements
▪ Know what the initial starting point (x,y) of
a rover and the direction (N,S,E,W) it is
facing.
▪ Implement commands that move the rover
forward/backward (f,b).
▪ Implement commands that turn the rover
left/right (l,r).
@pcameronpresley@pcameronpresley
Data Modeling
@pcameronpresley@pcameronpresley
What are the Nouns you heard in the
description?
Direction
Rover
Command
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
Modeling Rover
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
So a Rover has an X, a Y, and a Direction,
but how do I make developers provide
values?
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
Forced developers to specify necessary
information
Good, right?...
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
Immutability
@pcameronpresley
@pcameronpresley
@pcameronpresley
An immutable object is an
object whose state cannot be
modified after it is created
@pcameronpresley@pcameronpresley
Why?
@pcameronpresley@pcameronpresley
Keeping Track of
Changing State is
Hard…
@pcameronpresley@pcameronpresley
Requirements
▪ Know what the initial starting point (x,y) of
a rover and the direction (N,S,E,W) it is
facing.
▪ Implement commands that move the rover
forward/backward (f,b).
▪ Implement commands that turn the rover
left/right (l,r).
@pcameronpresley@pcameronpresley
Requirements
▪ Know what the initial starting point (x,y) of a
rover and the direction (N,S,E,W) it is facing.
▪ Implement commands that move the rover
forward/backward (f,b).
▪ Implement commands that turn the rover
left/right (l,r).
@pcameronpresley@pcameronpresley
When Moving Forward
If facing North, Y increases by 1
If facing South, Y decreases by 1
If facing East, X increases by 1
If facing West, X decreases by 1
@pcameronpresley@pcameronpresley
Design Implications
Since the rover is going to move, we need
to create a new state to represent the
rover….
@pcameronpresley@pcameronpresley
Rover
Rover.y+1
Rover.y-1
Rover.x+1
Rover.x-1
Rover
Facing North
Facing South
Facing East
Facing West
Move Forward Mapping
@pcameronpresley@pcameronpresley
Rover
Rover.y+1
Rover.y-1
Rover.x+1
Rover.x-1
Rover
Facing North
Facing South
Facing East
Facing West
Are there any directions in the first group that
aren’t mapped at all?
@pcameronpresley@pcameronpresley
Rover
Rover.y+1
Rover.y-1
Rover.x+1
Rover.x-1
Rover
Facing North
Facing South
Facing East
Facing West
Are there any directions in the first group that
aren’t mapped at all?
Are there any directions in the first group such
that they are mapped to two different outputs?
@pcameronpresley@pcameronpresley
Writing Functions
@pcameronpresley
@pcameronpresley
@pcameronpresley
A function is a mapping
between two sets such that
every element in the first
set maps to a single
element in the second set
@pcameronpresley@pcameronpresley
Months
February
April
January
March
…
# of Days
28
29
30
31
Mapping for Days in a Month
@pcameronpresley@pcameronpresley
Months
February
April
January
March
…
# of Days
28
29
30
31
Mapping for Days in a Month
@pcameronpresley@pcameronpresley
Months
January
February
…
December
Month #
1
2
…
12
13
Mapping for Month Number to Month
Argument
Exception
@pcameronpresley@pcameronpresley
Rover
Rover.y+1
Rover.y-1
Rover.x+1
Rover.x-1
Rover
Facing North
Facing South
Facing East
Facing West
Move Forward Mapping
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
Hard to debug
inconsistent
behavior
@pcameronpresley@pcameronpresley
Hard to use the
type system if the
types are lying…
@pcameronpresley@pcameronpresley
Requirements
▪ Know what the initial starting point (x,y) of a
rover and the direction (N,S,E,W) it is facing.
▪ Implement commands that move the rover
forward/backward (f,b).
▪ Implement commands that turn the rover
left/right (l,r).
@pcameronpresley@pcameronpresley
Requirements
▪ Know what the initial starting point (x,y) of a
rover and the direction (N,S,E,W) it is facing.
▪ Implement commands that move the rover
forward/backward (f,b).
▪ Implement commands that turn the rover
left/right (l,r).
@pcameronpresley@pcameronpresley
When Turning Left
If facing North, then faces West
If facing South, then faces East
If facing East, then faces North
If facing West, then faces South
@pcameronpresley@pcameronpresley
Design Implications
Since the rover is going to move, we need
to create a new state to represent the
rover….
@pcameronpresley@pcameronpresley
Rover
Now Faces West
Now Faces East
Now Faces North
Now Faces South
Rover
Facing North
Facing South
Facing East
Facing West
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
Requirements
▪ Know what the initial starting point (x,y) of a
rover and the direction (N,S,E,W) it is facing.
▪ Implement commands that move the rover
forward/backward (f,b).
▪ Implement commands that turn the rover
left/right (l,r).
@pcameronpresley@pcameronpresley
Requirements
▪ Know what the initial starting point (x,y) of a
rover and the direction (N,S,E,W) it is facing.
▪ Implement commands that move the rover
forward/backward (f,b).
▪ Implement commands that turn the rover
left/right (l,r).
@pcameronpresley@pcameronpresley
User Interaction
@pcameronpresley@pcameronpresley
User enters input
Input is mapped to a Command
Action is called with Rover
Command is mapped to an Action
Rover is updated with new state
@pcameronpresley@pcameronpresley
User enters input
Input is mapped to a Command
Action is called with Rover
Command is mapped to an Action
Rover is updated with new state
@pcameronpresley@pcameronpresley
All Strings
f, F
b, B
l, L
r, R
q, Q
Commands
MoveForward
MoveBackward
TurnLeft
TurnRight
Quit
Mapping User Input to Command
???
platypus
@pcameronpresley@pcameronpresley
Input can be any string, but
not all strings can be
mapped to a Command
@pcameronpresley@pcameronpresley
Restrict input to valid values
OR
Expand the possible outputs
@pcameronpresley@pcameronpresley
Introducing the Unknown
Command!
@pcameronpresley@pcameronpresley
All Strings
f, F
b, B
l, L
r, R
q, Q
platypus
Commands
MoveForward
MoveBackward
TurnLeft
TurnRight
Quit
Unknown
Mapping User Input to Command
???
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
User enters input
Input is mapped to a Command
Action is called with Rover
Command is mapped to an Action
Rover is updated with new state
@pcameronpresley@pcameronpresley
Rover => Rover
moveForward
moveBackward
turnLeft
turnRight
Commands
MoveForward
MoveBackward
TurnLeft
TurnRight
Quit
Unknown
@pcameronpresley@pcameronpresley
How should the
Rover state
update if told to
quit or don’t know
what to do?
@pcameronpresley@pcameronpresley
Do Nothing!
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
Rover => Rover
moveForward
moveBackward
turnLeft
turnRight
doNothing
Commands
MoveForward
MoveBackward
TurnLeft
TurnRight
Quit
Unknown
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
Bringing it all together:
Composition
@pcameronpresley@pcameronpresley
User enters input
Input is mapped to a Command
Action is called with Rover
Command is mapped to an Action
Rover is updated with new state
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
What if you already
knew all of the
commands ahead of
time?
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
Strings
f, F
b, B
l, L
r, R
q, Q
….
Commands
MoveForward
MoveBackward
TurnLeft
TurnRight
Quit
Unknown
Action
moveForward
moveBackward
turnLeft
turnRight
doNothing
@pcameronpresley@pcameronpresley
f, F
b, B
l, L
r, R
q, Q
….
Commands
MoveForward
MoveBackward
TurnLeft
TurnRight
Quit
Unknown
moveForward
moveBackward
turnLeft
turnRight
doNothing
ActionStrings
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
@pcameronpresley@pcameronpresley
Wrapping Up
Immutability removes the ability for
random access to internal state which
reduces complexity
@pcameronpresley@pcameronpresley
Wrapping Up
Functions are mappings from one type to
another such that for every element of
the first type, its mapped to a single
element of the second type
@pcameronpresley@pcameronpresley
Wrapping Up
Composition allows us to combine
smaller programs (functions) into larger
programs.
@pcameronpresley@pcameronpresley
Resources
▪ Videos
□ Professor Frisby Introduces Composable Functional JavaScript
▪ Articles
□ Thinking Functionally (Series) by Scott Wlaschin
▪ Books
□ Domain Modeling Made Functional by Scott Wlaschin
□ Professor Frisby's Mostly Adequate Guide to Functional
Programming by Brian Lonsdorf
□ Functional Light JavaScript by Kyle Simpson
▪ Code
□ FPThroughConstruction-Fundamentals (GitHub)
@pcameronpresley@pcameronpresley
Questions?
Email:Cameron@TheSoftwareMentor.com

More Related Content

Recently uploaded

%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Recently uploaded (20)

WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AI
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
 
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
 
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
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
 
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
 
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and ApplicationsWSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 

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...
 

Functional Programming Through Construction : First Principles

Editor's Notes

  1. Could model like this Any issues? For the C# version, would need to make sure that all fields were specified first.
  2. Anything wrong with this method? What about this part? That seems to be a bit weird… Based on the signature, I wouldn’t expect this to happen, but no way to enforce this…
  3. * Here’s the definition for Rover * Here’s the change we’ll make to prevent the issue from before (remove setter)
  4. Once we change the type, the “wrongness” starts to really be obvious
  5. Same object reference being used in multiple places… Parallel Processing Code
  6. Moving Forward can be thought of Given a rover A rover is returned If the given rover is facing north Then a rover is returned with a y that is one more than the previous
  7. In other words, is there a reason why we couldn’t get a Rover back for any Rover facing a direction? (Onto)
  8. In other words, is there any rover facing a direction where if we called this function, we’d get two different values? (one-to-one)
  9. Not a function because one element in the first set is mapped to two different elements
  10. Not a function because not ever element in the first set is mapped to an element in the second set
  11. Moving Forward can be thought of Given a rover A rover is returned If the given rover is facing north Then a rover is returned with a y that is one more than the previous
  12. If functions aren’t onto, that means there are use cases/results you have to “remember” to handle (no type system to help you out
  13. If functions aren’t onto, that means there are use cases/results you must “remember” to handle (no type system to help you out
  14. Any entry in the first set that has multiple outputs? Any entry in the first set that’s not mapped to the second set?
  15. Converting user input to Command
  16. Clean Architecture General Workflow Pattern (Data comes in, Gets Processed, Data Goes Out)
  17. How many times do we need to process a list? (4 times, 1 per Select and 1 for the Aggregate)
  18. Can only work if both functions to be composed are actually functions!
  19. By composing, we get the iterations down to 2 If you see a Select, followed by Aggregate (or a Map followed by a Reduce), we can combine the two!
  20. Not ideal, having to iterate the list 3 times