-
Notifications
You must be signed in to change notification settings - Fork 18
/
04-Api_FP_Core.fsx
180 lines (138 loc) · 5.06 KB
/
04-Api_FP_Core.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
(* ======================================
04-Api_FP_Core.fsx
Part of "Thirteen ways of looking at a turtle"
Related blog post: http://fsharpforfunandprofit.com/posts/13-ways-of-looking-at-a-turtle/
======================================
Way #4: 4: API (OO/FP hybrid approach) -- OO API calling stateless functions
In this design, an API layer communicates with pure turtle functions
and the client talks to the API layer.
The API layer manages the state (rather than the client) by storing a mutable turtle state.
*This approach has been named "Functional Core/Imperative Shell" by [Gary Bernhardt](https://www.youtube.com/watch?v=yTkzNHF6rMs)*
====================================== *)
#load "Common.fsx"
#load "FPTurtleLib.fsx"
#load "TurtleApiHelpers.fsx"
open Common
open FPTurtleLib
open TurtleApiHelpers // helpers for API validation, etc
// ======================================
// Turtle Api Layer
// ======================================
module TurtleApiLayer =
open Result
/// Function to log a message
let log message =
printfn "%s" message
// logged versions
let move = Turtle.move log
let turn = Turtle.turn log
let penDown = Turtle.penDown log
let penUp = Turtle.penUp log
let setColor = Turtle.setColor log
type TurtleApi() =
let mutable state = Turtle.initialTurtleState
/// Update the mutable state value
let updateState newState =
state <- newState
/// Execute the command string, and return a Result
/// Exec : commandStr:string -> Result<unit,ErrorMessage>
member this.Exec (commandStr:string) =
let tokens = commandStr.Split(' ') |> List.ofArray |> List.map trimString
// lift current state to Result
let stateR = returnR state
// calculate the new state
let newStateR =
match tokens with
| [ "Move"; distanceStr ] ->
// get the distance as a Result
let distanceR = validateDistance distanceStr
// call "move" lifted to the world of Results
lift2R move distanceR stateR
| [ "Turn"; angleStr ] ->
let angleR = validateAngle angleStr
lift2R turn angleR stateR
| [ "Pen"; "Up" ] ->
returnR (penUp state)
| [ "Pen"; "Down" ] ->
returnR (penDown state)
| [ "SetColor"; colorStr ] ->
let colorR = validateColor colorStr
lift2R setColor colorR stateR
| _ ->
Error (InvalidCommand commandStr)
// Lift `updateState` into the world of Results and
// call it with the new state.
Result.map updateState newStateR
// Return the final result (output of updateState)
// ======================================
// Turtle Api Client
// ======================================
module TurtleApiClient =
open TurtleApiLayer
open Result
let drawTriangle() =
let api = TurtleApi()
result {
do! api.Exec "Move 100"
do! api.Exec "Turn 120"
do! api.Exec "Move 100"
do! api.Exec "Turn 120"
do! api.Exec "Move 100"
do! api.Exec "Turn 120"
}
// back home at (0,0) with angle 0
let drawThreeLines() =
let api = TurtleApi()
result {
// draw black line
do! api.Exec "Pen Down"
do! api.Exec "SetColor Black"
do! api.Exec "Move 100"
// move without drawing
do! api.Exec "Pen Up"
do! api.Exec "Turn 90"
do! api.Exec "Move 100"
do! api.Exec "Turn 90"
// draw red line
do! api.Exec "Pen Down"
do! api.Exec "SetColor Red"
do! api.Exec "Move 100"
// move without drawing
do! api.Exec "Pen Up"
do! api.Exec "Turn 90"
do! api.Exec "Move 100"
do! api.Exec "Turn 90"
// back home at (0,0) with angle 0
// draw diagonal blue line
do! api.Exec "Pen Down"
do! api.Exec "SetColor Blue"
do! api.Exec "Turn 45"
do! api.Exec "Move 100"
}
let drawPolygon n =
let angle = 180.0 - (360.0/float n)
let api = TurtleApi()
// define a function that draws one side
let drawOneSide() = result {
do! api.Exec "Move 100.0"
do! api.Exec (sprintf "Turn %f" angle)
}
// repeat for all sides
result {
for i in [1..n] do
do! drawOneSide()
}
let triggerError() =
let api = TurtleApi()
api.Exec "Move bad"
// ======================================
// Turtle Api Tests
// ======================================
(*
TurtleApiClient.drawTriangle()
TurtleApiClient.drawThreeLines()
TurtleApiClient.drawPolygon 4
// test errors
TurtleApiClient.triggerError()
// Error (InvalidDistance "bad")
*)