F#: Implementing Business Rules Kata (Vol.2)

I rewrote a kata after being inspired by Mark Seeman’s answer:

module BusinessRulesImpl

(*Types*)
type Plan = Standard | Platnum

type Command =
    | IssueSlip     of string
    | EmailOwner    of Plan
    | PayCommission of string
    | AttachVideo   of VideoType

and VideoType =
    | FirstAid
    | Other    of string

and Product =
    | Physical
    | Book
    | Video             of VideoType
    | StartMembership   of Plan
    | UpgradeMembership

(*Functions*)
let handleShipping = function
    | Physical | Book | Video _ -> [IssueSlip "Shipping"]
    | _ -> []

let handleRoyalty = function
    | Book -> [IssueSlip "Royalty Dept."]
    | _ -> []

let handleCommission = function
    | Physical | Book | Video _ -> [PayCommission "Agent X"]
    | _ -> []

let handleVideo = function
    | Video kind when kind = FirstAid -> [AttachVideo FirstAid]
    | _ -> []

let handleEmail = function
    | StartMembership   plan -> [EmailOwner plan]
    | UpgradeMembership      -> [EmailOwner Platnum]
    | _ -> []

let handleAll handlers item =
    handlers |> List.collect(fun handler -> handler item)

let handlers = [
    handleShipping
    handleRoyalty
    handleCommission
    handleVideo
    handleEmail]

(*Client*)
let result = (handlers , StartMembership Standard) ||> handleAll
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: