-
Notifications
You must be signed in to change notification settings - Fork 70
/
Light.fs
33 lines (31 loc) · 988 Bytes
/
Light.fs
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
/// By Jérémie Chassaing / @thinkb4coding
/// https://github.com/dddeu/dddeu20-talks-jeremie-chassaing-functional-event-sourcing/blob/main/EventSourcing.fsx#L52-L84
module Domain.Light
type Event =
| SwitchedOn
| SwitchedOff
| Broke
type State =
| Working of CurrentState
| Broken
and CurrentState = { on: bool; remainingUses: int }
let initial = Working { on = false; remainingUses = 3 }
let private evolve s e =
match s with
| Broken -> s
| Working s ->
match e with
| SwitchedOn -> Working { on = true; remainingUses = s.remainingUses - 1 }
| SwitchedOff -> Working { s with on = false }
| Broke -> Broken
let fold = Array.fold evolve
let decideSwitch (on: bool) s = [|
match s with
| Broken -> ()
| Working { on = true } ->
if not on then
SwitchedOff
| Working { on = false; remainingUses = r } ->
if on then
if r = 0 then Broke
else SwitchedOn |]