SlideShare a Scribd company logo
@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

PDF
October 19, Probabilistic Modeling III
PDF
Please create a simple flowchart of this programtell me the necess.pdf
PPT
Practices of robotics with ROBOLAB and LEGO RCX ROBOT
PDF
Optimized Multi-agent Box-pushing - 2017-10-24
DOCX
ROVER PROJECT
DOCX
Arduino Final Project
PPT
AI Robotics
PDF
MSI UI Software Design Report
October 19, Probabilistic Modeling III
Please create a simple flowchart of this programtell me the necess.pdf
Practices of robotics with ROBOLAB and LEGO RCX ROBOT
Optimized Multi-agent Box-pushing - 2017-10-24
ROVER PROJECT
Arduino Final Project
AI Robotics
MSI UI Software Design Report

Similar to Functional Programming Through Construction : First Principles (20)

PDF
nd209_Robo_syllabus_v2.pdf
PDF
Implementation of D* Path Planning Algorithm with NXT LEGO Mindstorms Kit for...
PDF
Mars rover-extension
PDF
Effective Robotics Programming with ROS Anil Mahtani
PPTX
FIRSTFare 2012 advanced lab view topics
PDF
MAKING OF LINE FOLLOWER ROBOT
PPTX
Teacher slides_3.pptx
DOCX
System Document (Revised)
PDF
Effective Robotics Programming with ROS Third Edition Anil Mahtani Luis Sanch...
PDF
Effective Robotics Programming with ROS Third Edition Anil Mahtani Luis Sanch...
PDF
Building a-line-following-robot
PDF
Building a-line-following-robot
DOCX
Final Lab, Arduino Robot Challenge
PDF
Goal The goal is to build a Maze-runner special edition of your .pdf
PDF
Robotics Navigation
PPTX
VEX_PPT
PDF
Grid Based Autonomous Navigator
PDF
Introduction to robotics and the advantages
PDF
Mechanism Design and Some Challenges from Perseverance CL24_6225.pdf
DOC
Weekly Report 5 7
nd209_Robo_syllabus_v2.pdf
Implementation of D* Path Planning Algorithm with NXT LEGO Mindstorms Kit for...
Mars rover-extension
Effective Robotics Programming with ROS Anil Mahtani
FIRSTFare 2012 advanced lab view topics
MAKING OF LINE FOLLOWER ROBOT
Teacher slides_3.pptx
System Document (Revised)
Effective Robotics Programming with ROS Third Edition Anil Mahtani Luis Sanch...
Effective Robotics Programming with ROS Third Edition Anil Mahtani Luis Sanch...
Building a-line-following-robot
Building a-line-following-robot
Final Lab, Arduino Robot Challenge
Goal The goal is to build a Maze-runner special edition of your .pdf
Robotics Navigation
VEX_PPT
Grid Based Autonomous Navigator
Introduction to robotics and the advantages
Mechanism Design and Some Challenges from Perseverance CL24_6225.pdf
Weekly Report 5 7
Ad

More from Cameron Presley (10)

PPTX
The Engineer's Playbook: Starting a New Role
PPTX
Taking a Gamble with Functional Domain Modeling
PPTX
Level Up Your Functional Programming Skills with LINQ
PPTX
Establishing a SOLID Foundation
PPTX
How Functional Programming Made Me a Better Developer
PPTX
How to Have Code Reviews That Developers Actually Want
PPTX
Making the Unstable Stable - An Intro To Testing
PPTX
Indy Code - Taking a Gamble With F#: Implementing Blackjack
PPTX
How Functional Programming Made Me A Better Developer
PPTX
Establishing a SOLID Foundation - An Intro to Software Design
The Engineer's Playbook: Starting a New Role
Taking a Gamble with Functional Domain Modeling
Level Up Your Functional Programming Skills with LINQ
Establishing a SOLID Foundation
How Functional Programming Made Me a Better Developer
How to Have Code Reviews That Developers Actually Want
Making the Unstable Stable - An Intro To Testing
Indy Code - Taking a Gamble With F#: Implementing Blackjack
How Functional Programming Made Me A Better Developer
Establishing a SOLID Foundation - An Intro to Software Design
Ad

Recently uploaded (20)

PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
ai tools demonstartion for schools and inter college
PPTX
Transform Your Business with a Software ERP System
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
Digital Strategies for Manufacturing Companies
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Introduction to Artificial Intelligence
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
System and Network Administraation Chapter 3
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
assetexplorer- product-overview - presentation
PPT
Introduction Database Management System for Course Database
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
ai tools demonstartion for schools and inter college
Transform Your Business with a Software ERP System
Designing Intelligence for the Shop Floor.pdf
Digital Strategies for Manufacturing Companies
Operating system designcfffgfgggggggvggggggggg
Introduction to Artificial Intelligence
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
System and Network Administraation Chapter 3
How to Migrate SBCGlobal Email to Yahoo Easily
Upgrade and Innovation Strategies for SAP ERP Customers
Softaken Excel to vCard Converter Software.pdf
Reimagine Home Health with the Power of Agentic AI​
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
assetexplorer- product-overview - presentation
Introduction Database Management System for Course Database

Functional Programming Through Construction : First Principles

Editor's Notes

  • #15: Could model like this Any issues? For the C# version, would need to make sure that all fields were specified first.
  • #19: 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…
  • #20: * Here’s the definition for Rover * Here’s the change we’ll make to prevent the issue from before (remove setter)
  • #21: Once we change the type, the “wrongness” starts to really be obvious
  • #25: Same object reference being used in multiple places… Parallel Processing Code
  • #30: 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
  • #31: In other words, is there a reason why we couldn’t get a Rover back for any Rover facing a direction? (Onto)
  • #32: 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)
  • #36: Not a function because one element in the first set is mapped to two different elements
  • #37: Not a function because not ever element in the first set is mapped to an element in the second set
  • #38: 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
  • #41: 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
  • #42: If functions aren’t onto, that means there are use cases/results you must “remember” to handle (no type system to help you out
  • #47: Any entry in the first set that has multiple outputs? Any entry in the first set that’s not mapped to the second set?
  • #60: Converting user input to Command
  • #77: Clean Architecture General Workflow Pattern (Data comes in, Gets Processed, Data Goes Out)
  • #78: How many times do we need to process a list? (4 times, 1 per Select and 1 for the Aggregate)
  • #80: Can only work if both functions to be composed are actually functions!
  • #82: 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!
  • #86: Not ideal, having to iterate the list 3 times