Skip to content

Instantly share code, notes, and snippets.

@adamchester
Last active July 4, 2018 00:25
Show Gist options
  • Save adamchester/11ccbcf9e6b1d3fc8ef7 to your computer and use it in GitHub Desktop.
Save adamchester/11ccbcf9e6b1d3fc8ef7 to your computer and use it in GitHub Desktop.
Describing a state machine / approval workflow in F#
type State = Initial | Draft | PendingApproval | Approved | Cancelled | Completed
type Action = Create | Cancel | SendForApproval | Approve | Reject | Complete
type Role = Creator | Approver | Completor
type Transition = { Action:Action; From:State; To:State; Roles:Role list }
module Transitions =
let create = { Action=Create; From=Initial; To=Draft; Roles=[ Creator ] }
let cancel = { Action=Cancel; From=Draft; To=Cancelled; Roles=[ Creator ] }
let sendForAppr = { Action=SendForApproval; From=Draft; To=PendingApproval; Roles=[ Creator ] }
let approve = { Action=Approve; From=PendingApproval; To=Approved; Roles=[ Approver ] }
let reject = { Action=Reject; From=PendingApproval; To=Draft; Roles=[ Approver ] }
let complete = { Action=Complete; From=Approved; To=Completed; Roles=[ Completor ] }
let all = [ create; sendForAppr; approve; reject; complete; cancel; ]
Transitions.all
|> List.mapi (fun i t -> i, t.Action, t.From, t.To, t.Roles)
(*
val it : (int * Action * State * State * Role list) list =
[(0, Create, Initial, Draft, [Creator]);
(1, SendForApproval, Draft, PendingApproval, [Creator]);
(2, Approve, PendingApproval, Approved, [Approver]);
(3, Reject, PendingApproval, Draft, [Approver]);
(4, Complete, Approved, Completed, [Completor]);
(5, Cancel, Draft, Cancelled, [Creator])]
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment