Skip to content

Instantly share code, notes, and snippets.

@vilinski
Last active December 26, 2019 04:58
Show Gist options
  • Save vilinski/ba94e2f510abe2637020d51a93da51e2 to your computer and use it in GitHub Desktop.
Save vilinski/ba94e2f510abe2637020d51a93da51e2 to your computer and use it in GitHub Desktop.
open System
type User = string
type Title = string
type BugState =
| New
| Assigned of User
| Deferred
| Closed
type Bug =
{ Id : Guid
Title : Title
State : BugState
}
module Bug =
let create title =
{ Id = Guid.NewGuid()
Title = title
State = New }
let sendEmailToAssignee assignee message bug =
printfn "@%s, RE %s: %s" assignee bug.Title message
bug
type Trigger =
| Assign of User
| Defer
| Close
let FSM trigger bug =
match bug.State, trigger with
| Assigned oldUser, Assign newUser when oldUser <> newUser ->
{ bug with State = Assigned newUser }
|> sendEmailToAssignee oldUser "Don't forget to help the new employee!"
|> sendEmailToAssignee newUser "You own it."
| New, Assign user
| Deferred, Assign user ->
{ bug with State = Assigned user }
|> sendEmailToAssignee user "You own it."
| New, Defer ->
{ bug with State = Deferred }
| Assigned user, Defer ->
sendEmailToAssignee user "You're off the hook."
{ bug with State = Deferred }
| Assigned _, Close ->
{ bug with State = Closed }
| _, _ ->
printfn "ERROR: Can't apply trigger %A to ticket in state %A" trigger bug.State
bug
Bug.create "Incorrect stock count"
|> FSM (Assign "Joe")
|> FSM Defer
|> FSM (Assign "Harry")
|> FSM (Assign "Fred")
|> FSM Close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment