-
Notifications
You must be signed in to change notification settings - Fork 18
/
08-StateMonad.fsx
202 lines (155 loc) · 5.65 KB
/
08-StateMonad.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
(* ======================================
08-StateMonad.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 #8: Batch oriented -- Using a state monad (computation expression)
In this design, the client uses the FP Turtle functions directly.
As before, the client must keep track of the current state and pass it into the next function call,
but this time the state is kept out of sight by using a State monad (called `turtle` computation expression here)
As a result, there are no mutables anywhere.
====================================== *)
#load "Common.fsx"
#load "FPTurtleLib.fsx"
open Common
open FPTurtleLib
// ======================================
// TurtleStateComputation
// ======================================
/// Create a type to wrap a function like:
/// oldState -> (a,newState)
type TurtleStateComputation<'a> =
TurtleStateComputation of (Turtle.TurtleState -> 'a * Turtle.TurtleState)
/// Functions that work with TurtleStateComputation
module TurtleStateComputation =
let runT turtle state =
// pattern match against the turtle
// to extract the inner function
let (TurtleStateComputation innerFn) = turtle
// run the inner function with the passed in state
innerFn state
let returnT x =
let innerFn state =
(x,state)
TurtleStateComputation innerFn
let bindT f xT =
let innerFn state =
let x,state2 = runT xT state
runT (f x) state2
TurtleStateComputation innerFn
let mapT f =
bindT (f >> returnT)
let toComputation f =
let innerFn state =
let (result,newState) = f state
(result,newState)
TurtleStateComputation innerFn
let toUnitComputation f =
let f2 state =
(),f state
toComputation f2
// define a computation expression builder
type TurtleBuilder() =
member this.Return(x) = returnT x
member this.Bind(x,f) = bindT f x
// create an instance of the computation expression builder
let turtle = TurtleBuilder()
// ======================================
// TurtleComputationClient
// ======================================
module TurtleComputationClient =
open TurtleStateComputation
open Result
/// Function to log a message
let log message =
printfn "%s" message
let initialTurtleState =
Turtle.initialTurtleState
// ----------------------------------------
// monadic versions of the Turtle functions
// ----------------------------------------
let move dist =
toUnitComputation (Turtle.move log dist)
// val move : Distance -> TurtleStateComputation<unit>
let turn angle =
toUnitComputation (Turtle.turn log angle)
// val turn : Angle -> TurtleStateComputation<unit>
let penDown =
toUnitComputation (Turtle.penDown log)
// val penDown : TurtleStateComputation<unit>
let penUp =
toUnitComputation (Turtle.penUp log)
// val penUp : TurtleStateComputation<unit>
let setColor color =
toUnitComputation (Turtle.setColor log color)
// val setColor : PenColor -> TurtleStateComputation<unit>
// ----------------------------------------
// draw various things
// ----------------------------------------
let drawTriangle() =
// define a set of instructions
let t = turtle {
do! move 100.0
do! turn 120.0<Degrees>
do! move 100.0
do! turn 120.0<Degrees>
do! move 100.0
do! turn 120.0<Degrees>
}
// finally, run them using the initial state as input
runT t initialTurtleState
let drawThreeLines() =
// define a set of instructions
let t = turtle {
// draw black line
do! penDown
do! setColor Black
do! move 100.0
// move without drawing
do! penUp
do! turn 90.0<Degrees>
do! move 100.0
do! turn 90.0<Degrees>
// draw red line
do! penDown
do! setColor Red
do! move 100.0
// move without drawing
do! penUp
do! turn 90.0<Degrees>
do! move 100.0
do! turn 90.0<Degrees>
// back home at (0,0) with angle 0
// draw diagonal blue line
do! penDown
do! setColor Blue
do! turn 45.0<Degrees>
do! move 100.0
}
// finally, run them using the initial state
runT t initialTurtleState
let drawPolygon n =
let angle = 180.0 - (360.0/float n)
let angleDegrees = angle * 1.0<Degrees>
// define a function that draws one side
let oneSide = turtle {
do! move 100.0
do! turn angleDegrees
}
// chain two turtle operations in sequence
let chain f g = turtle {
do! f
do! g
}
// create a list of operations, one for each side
let sides = List.replicate n oneSide
// chain all the sides into one operation
let all = sides |> List.reduce chain
// finally, run them using the initial state
runT all initialTurtleState
// ======================================
// Turtle Computation Tests
// ======================================
TurtleComputationClient.drawTriangle()
TurtleComputationClient.drawThreeLines()
TurtleComputationClient.drawPolygon 4