Skip to content

Commit d824c1f

Browse files
committed
refactor part6
1 parent 93e6915 commit d824c1f

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

fsharp/HopacSeries/Part6/timebomb.fsx

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,47 +42,49 @@ type TimeBomb () =
4242
|> start
4343

4444
let rec inputLoop defuseChar =
45-
inCh
46-
|> Alt.afterJob (fun c ->
47-
if c = defuseChar then
45+
let onInput inChar =
46+
if inChar = defuseChar then
4847
IVar.tryFill reason Defused
4948
else
5049
inputLoop defuseChar :> Job<unit>
51-
)
50+
inCh
51+
|> Alt.afterJob onInput
5252
let activate seconds defuseChar =
5353
let ticker = startTicker seconds
5454
startTimeOut seconds
5555
IVar.tryFill activated () |> start
5656
inputLoop defuseChar |> start
5757
IVar.read reason
5858
|> Alt.afterFun (fun _ -> ticker.Stop())
59+
|> start
5960

60-
member __.Status () =
61-
let deadReasonAlt =
62-
IVar.read reason
63-
|> Alt.afterFun Dead
64-
65-
let activatedAlt =
66-
IVar.read activated
67-
|> Alt.afterFun (fun _ -> Alive)
68-
69-
let notActivatedAlt =
70-
Alt.always NotActivated
71-
72-
Alt.choose [
73-
deadReasonAlt
74-
activatedAlt
75-
notActivatedAlt
76-
] |> run
61+
member __.Status
62+
with get() =
63+
let deadReasonAlt =
64+
IVar.read reason
65+
|> Alt.afterFun Dead
66+
67+
let activatedAlt =
68+
IVar.read activated
69+
|> Alt.afterFun (fun _ -> Alive)
70+
71+
let notActivatedAlt =
72+
Alt.always NotActivated
73+
74+
Alt.choose [
75+
deadReasonAlt
76+
activatedAlt
77+
notActivatedAlt]
78+
|> run
7779

7880
member this.Activate (seconds : int, defuseChar : char) =
79-
match this.Status() with
81+
match this.Status with
8082
| NotActivated ->
81-
activate seconds defuseChar |> start
83+
activate seconds defuseChar
8284
| _ -> ()
8385

8486
member this.TryDefuse(defuseChar) =
85-
match this.Status() with
87+
match this.Status with
8688
| Alive ->
8789
Ch.give inCh defuseChar
8890
|> start
@@ -103,21 +105,21 @@ let printSecondsRemaining (t : TimeBomb) =
103105
let simulateExplosion () =
104106
let seconds = 5
105107
let t = TimeBomb()
106-
t.Status() |> printfn "Status: %A"
108+
t.Status |> printfn "Status: %A"
107109
t.Activate(seconds, 'a')
108110
printSecondsRemaining t
109-
t.Status() |> printfn "Status: %A"
111+
t.Status |> printfn "Status: %A"
110112
t.DeadStatusAlt
111113
|> Alt.afterFun (printfn "Status: %A")
112114
|> run
113115

114116
let simulateDefusal char =
115117
let seconds = 5
116118
let t = TimeBomb()
117-
t.Status() |> printfn "Status: %A"
119+
t.Status |> printfn "Status: %A"
118120
t.Activate(seconds, 'a')
119121
printSecondsRemaining t
120-
t.Status() |> printfn "Status: %A"
122+
t.Status |> printfn "Status: %A"
121123

122124
TimeSpan.FromSeconds 3.
123125
|> timeOut

0 commit comments

Comments
 (0)