Successfully reported this slideshow.

×

# Functional Programming Through Construction : First Principles

Presentation given at DevSpace 2019 for FP Through Construction

Presentation given at DevSpace 2019 for FP Through Construction

## More Related Content

### Functional Programming Through Construction : First Principles

1. 1. @pcameronpresley@pcameronpresley LEARNING FP THROUGH CONSTRUCTION: FIRST PRINCIPLES Cameron Presley @pcameronpresley Cameron@TheSoftwareMentor.com Slides can be found at: https://blog.TheSoftwareMentor.com/Presentations
2. 2. @pcameronpresley
3. 3. @pcameronpresley@pcameronpresley Hello!
4. 4. @pcameronpresley@pcameronpresley Outline ▪ Introduction to Mars Rover Kata ▪ Determine data models ▪ Determine functions ▪ Wire everything together
5. 5. @pcameronpresley@pcameronpresley 5
6. 6. @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.
7. 7. @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).
8. 8. @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).
9. 9. @pcameronpresley@pcameronpresley Data Modeling
10. 10. @pcameronpresley@pcameronpresley What are the Nouns you heard in the description? Direction Rover Command
11. 11. @pcameronpresley@pcameronpresley
12. 12. @pcameronpresley@pcameronpresley
13. 13. @pcameronpresley@pcameronpresley Modeling Rover
14. 14. @pcameronpresley@pcameronpresley
15. 15. @pcameronpresley@pcameronpresley So a Rover has an X, a Y, and a Direction, but how do I make developers provide values?
16. 16. @pcameronpresley@pcameronpresley
17. 17. @pcameronpresley@pcameronpresley Forced developers to specify necessary information Good, right?...
18. 18. @pcameronpresley@pcameronpresley
19. 19. @pcameronpresley@pcameronpresley
20. 20. @pcameronpresley@pcameronpresley
21. 21. @pcameronpresley@pcameronpresley Immutability
22. 22. @pcameronpresley @pcameronpresley @pcameronpresley An immutable object is an object whose state cannot be modified after it is created
23. 23. @pcameronpresley@pcameronpresley Why?
24. 24. @pcameronpresley@pcameronpresley Keeping Track of Changing State is Hard…
25. 25. @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).
26. 26. @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).
27. 27. @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
28. 28. @pcameronpresley@pcameronpresley Design Implications Since the rover is going to move, we need to create a new state to represent the rover….
29. 29. @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
30. 30. @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?
31. 31. @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?
32. 32. @pcameronpresley@pcameronpresley Writing Functions
33. 33. @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
34. 34. @pcameronpresley@pcameronpresley Months February April January March … # of Days 28 29 30 31 Mapping for Days in a Month
35. 35. @pcameronpresley@pcameronpresley Months February April January March … # of Days 28 29 30 31 Mapping for Days in a Month
36. 36. @pcameronpresley@pcameronpresley Months January February … December Month # 1 2 … 12 13 Mapping for Month Number to Month Argument Exception
37. 37. @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
38. 38. @pcameronpresley@pcameronpresley
39. 39. @pcameronpresley@pcameronpresley
40. 40. @pcameronpresley@pcameronpresley Hard to debug inconsistent behavior
41. 41. @pcameronpresley@pcameronpresley Hard to use the type system if the types are lying…
42. 42. @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).
43. 43. @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).
44. 44. @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
45. 45. @pcameronpresley@pcameronpresley Design Implications Since the rover is going to move, we need to create a new state to represent the rover….
46. 46. @pcameronpresley@pcameronpresley Rover Now Faces West Now Faces East Now Faces North Now Faces South Rover Facing North Facing South Facing East Facing West
47. 47. @pcameronpresley@pcameronpresley
48. 48. @pcameronpresley@pcameronpresley
49. 49. @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).
50. 50. @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).
51. 51. @pcameronpresley@pcameronpresley User Interaction
52. 52. @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
53. 53. @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
54. 54. @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
55. 55. @pcameronpresley@pcameronpresley Input can be any string, but not all strings can be mapped to a Command
56. 56. @pcameronpresley@pcameronpresley Restrict input to valid values OR Expand the possible outputs
57. 57. @pcameronpresley@pcameronpresley Introducing the Unknown Command!
58. 58. @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 ???
59. 59. @pcameronpresley@pcameronpresley
60. 60. @pcameronpresley@pcameronpresley
61. 61. @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
62. 62. @pcameronpresley@pcameronpresley Rover => Rover moveForward moveBackward turnLeft turnRight Commands MoveForward MoveBackward TurnLeft TurnRight Quit Unknown
63. 63. @pcameronpresley@pcameronpresley How should the Rover state update if told to quit or don’t know what to do?
64. 64. @pcameronpresley@pcameronpresley Do Nothing!
65. 65. @pcameronpresley@pcameronpresley
66. 66. @pcameronpresley@pcameronpresley Rover => Rover moveForward moveBackward turnLeft turnRight doNothing Commands MoveForward MoveBackward TurnLeft TurnRight Quit Unknown
67. 67. @pcameronpresley@pcameronpresley
68. 68. @pcameronpresley@pcameronpresley
69. 69. @pcameronpresley@pcameronpresley Bringing it all together: Composition
70. 70. @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
71. 71. @pcameronpresley@pcameronpresley
72. 72. @pcameronpresley@pcameronpresley What if you already knew all of the commands ahead of time?
73. 73. @pcameronpresley@pcameronpresley
74. 74. @pcameronpresley@pcameronpresley
75. 75. @pcameronpresley@pcameronpresley
76. 76. @pcameronpresley@pcameronpresley
77. 77. @pcameronpresley@pcameronpresley
78. 78. @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
79. 79. @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
80. 80. @pcameronpresley@pcameronpresley
81. 81. @pcameronpresley@pcameronpresley
82. 82. @pcameronpresley@pcameronpresley Wrapping Up Immutability removes the ability for random access to internal state which reduces complexity
83. 83. @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
84. 84. @pcameronpresley@pcameronpresley Wrapping Up Composition allows us to combine smaller programs (functions) into larger programs.
85. 85. @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)
86. 86. @pcameronpresley@pcameronpresley Questions? Email:Cameron@TheSoftwareMentor.com

### Editor's Notes

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