SlideShare a Scribd company logo
1 of 86
Download to read offline
@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

What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesVictoriaMetrics
 
Key Steps in Agile Software Delivery Roadmap
Key Steps in Agile Software Delivery RoadmapKey Steps in Agile Software Delivery Roadmap
Key Steps in Agile Software Delivery RoadmapIshara Amarasekera
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...OnePlan Solutions
 
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...kalichargn70th171
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfmaor17
 
logical backup of Oracle Datapump-detailed.pptx
logical backup of Oracle Datapump-detailed.pptxlogical backup of Oracle Datapump-detailed.pptx
logical backup of Oracle Datapump-detailed.pptxRemote DBA Services
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slidesvaideheekore1
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldRoberto Pérez Alcolea
 
oracle 23c new features for developer and dba
oracle 23c new features for developer and dbaoracle 23c new features for developer and dba
oracle 23c new features for developer and dbaRemote DBA Services
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogueitservices996
 
Advantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptxAdvantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptxRTS corp
 
Mastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptxMastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptxAS Design & AST.
 
Revolutionize Your Video Editing with InVideo.io: A Comprehensive Review
Revolutionize Your Video Editing with InVideo.io: A Comprehensive ReviewRevolutionize Your Video Editing with InVideo.io: A Comprehensive Review
Revolutionize Your Video Editing with InVideo.io: A Comprehensive Reviewjw364beach
 
full course of software engineering mid term.pdf
full course of software engineering mid term.pdffull course of software engineering mid term.pdf
full course of software engineering mid term.pdfAbdul salam
 
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...OnePlan Solutions
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolsosttopstonverter
 
Santander Stream Processing with Apache Flink
Santander Stream Processing with Apache FlinkSantander Stream Processing with Apache Flink
Santander Stream Processing with Apache Flinkconfluent
 
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdfAndrey Devyatkin
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfRTS corp
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsJean Silva
 

Recently uploaded (20)

What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 Updates
 
Key Steps in Agile Software Delivery Roadmap
Key Steps in Agile Software Delivery RoadmapKey Steps in Agile Software Delivery Roadmap
Key Steps in Agile Software Delivery Roadmap
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
 
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdf
 
logical backup of Oracle Datapump-detailed.pptx
logical backup of Oracle Datapump-detailed.pptxlogical backup of Oracle Datapump-detailed.pptx
logical backup of Oracle Datapump-detailed.pptx
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slides
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository world
 
oracle 23c new features for developer and dba
oracle 23c new features for developer and dbaoracle 23c new features for developer and dba
oracle 23c new features for developer and dba
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogue
 
Advantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptxAdvantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptx
 
Mastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptxMastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptx
 
Revolutionize Your Video Editing with InVideo.io: A Comprehensive Review
Revolutionize Your Video Editing with InVideo.io: A Comprehensive ReviewRevolutionize Your Video Editing with InVideo.io: A Comprehensive Review
Revolutionize Your Video Editing with InVideo.io: A Comprehensive Review
 
full course of software engineering mid term.pdf
full course of software engineering mid term.pdffull course of software engineering mid term.pdf
full course of software engineering mid term.pdf
 
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration tools
 
Santander Stream Processing with Apache Flink
Santander Stream Processing with Apache FlinkSantander Stream Processing with Apache Flink
Santander Stream Processing with Apache Flink
 
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero results
 

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