Last active
February 2, 2018 16:04
-
-
Save anthonyshull/9b83b0bac9b89210af108f9f10141515 to your computer and use it in GitHub Desktop.
Domain Modeling with F#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Microsoft.FSharp.Data.UnitSystems.SI.UnitNames | |
type Supply = Supply of float<watt> | |
let Supply (electricity: float<watt>) = | |
if electricity > 0.0<watt> then | |
Some (Supply electricity) | |
else | |
None | |
type PowerStation = { | |
Name: string | |
Supply: Supply option | |
Coordinates: float * float | |
} | |
type TransmissionSubstation = { | |
Name: string | |
Supply: Supply option | |
Coordinates: float * float | |
} | |
type Station = PowerStation of PowerStation | TransmissionSubstation of TransmissionSubstation | |
type Generate = PowerStation -> Supply option -> PowerStation | |
let generate (powerStation: PowerStation) (supply: Supply option): PowerStation = | |
match powerStation.Supply, supply with | |
| Some (Supply powerStationSupply), Some (Supply supply) -> | |
{ powerStation with Supply = Supply (powerStationSupply + supply) } | |
| None, Some (Supply supply) -> | |
{ powerStation with Supply = (Supply supply) } | |
| Some _ , None | _, _ -> powerStation | |
type Distribute = TransmissionSubstation -> Supply option -> TransmissionSubstation | |
let distribute (substation: TransmissionSubstation) (supply: Supply option): TransmissionSubstation = | |
match substation.Supply, supply with | |
| Some (Supply substationSupply), Some (Supply supply) -> | |
{ substation with Supply = Supply (substationSupply - supply) } | |
| None, _ -> | |
{ substation with Supply = None } | |
| _, _ -> substation | |
type Transmit = Station -> Station -> Supply option -> Station * Station | |
let transmit (fStation: Station) (tStation: Station) (supply: Supply option) = | |
match fStation, tStation, supply with | |
| PowerStation fStation, TransmissionSubstation tStation, Some (Supply supply) -> | |
match fStation.Supply, tStation.Supply with | |
| Some (Supply fStationSupply), Some (Supply tStationSupply) -> | |
if fStationSupply > supply then | |
PowerStation { fStation with Supply = Supply (fStationSupply - supply) }, | |
TransmissionSubstation { tStation with Supply = Supply (tStationSupply + supply) } | |
else | |
PowerStation { fStation with Supply = None }, | |
TransmissionSubstation { tStation with Supply = Supply (tStationSupply + fStationSupply)} | |
| None, _ | _, _ -> PowerStation fStation, TransmissionSubstation tStation | |
| TransmissionSubstation fStation, TransmissionSubstation tStation, Some (Supply supply) -> | |
match fStation.Supply, tStation.Supply with | |
| Some (Supply fStationSupply), Some (Supply tStationSupply) -> | |
if fStationSupply > supply then | |
TransmissionSubstation { fStation with Supply = Supply (fStationSupply - supply) }, | |
TransmissionSubstation { tStation with Supply = Supply (tStationSupply + supply) } | |
else | |
TransmissionSubstation { fStation with Supply = None }, | |
TransmissionSubstation { tStation with Supply = Supply (tStationSupply + fStationSupply)} | |
| None, _ | _, _ -> TransmissionSubstation fStation, TransmissionSubstation tStation | |
| _, _, None | _, _, _ -> fStation, tStation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment