Skip to content

Instantly share code, notes, and snippets.

@isaacabraham
Created April 9, 2022 19:46
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacabraham/6beee319f256fd15eb44e77ec6ce3b47 to your computer and use it in GitHub Desktop.
Save isaacabraham/6beee319f256fd15eb44e77ec6ce3b47 to your computer and use it in GitHub Desktop.
open System
/// A train carriage can have a number of different features...
type Feature = Quiet | Wifi | Toilet
/// Multiple classes
type CarriageClass = First | Second
/// Carriages can be either for passengers or the buffet cart
type CarriageKind =
| Passenger of CarriageClass
| Buffet of {| ColdFood : bool; HotFood : bool |}
/// A carriage has a unique number on the train
type CarriageNumber = CarriageNumber of int
/// A carriage has a number, kind, a list of features and a finite number of seats.
type Carriage =
{
Number : CarriageNumber
Kind : CarriageKind
Features : Feature Set
NumberOfSeats : int
}
type TrainId = TrainId of string
type Station = Station of string
/// Each stop is a station and a time of arrival
type Stop = Station * TimeOnly
/// A train has a unique id, and a list of carriages. It always has an origin and destination,
/// and may a list of stops in between. It *might* also have a station where the driver changes.
type Train =
{
Id : TrainId
Carriages : Carriage list
Origin : Stop
Stops : Stop list
Destination : Stop
DriverChange : Station option
}
/// A function that calculates the total number of seats in a train
let totalNumberOfSeats train =
train.Carriages |> List.sumBy (fun carriage -> carriage.NumberOfSeats)
let exampleTrain =
{
Id = TrainId "ABC123"
Carriages = [
{ Number = CarriageNumber 1; Kind = Passenger First; Features = Set [ Wifi; Quiet; Toilet ]; NumberOfSeats = 45 }
{ Number = CarriageNumber 2; Kind = Passenger Second; Features = Set [ Toilet ]; NumberOfSeats = 65 }
{ Number = CarriageNumber 3; Kind = Buffet {| ColdFood = true; HotFood = true |}; Features = Set [ Wifi ]; NumberOfSeats = 12 }
]
Origin = Station "London St Pancras", TimeOnly (9,00)
Stops = [
Station "Ashford", TimeOnly (10,30)
Station "Lille", TimeOnly (11,30)
]
Destination = Station "Paris Nord", TimeOnly (12,15)
DriverChange = Some (Station "Ashford")
}
let exampleTrainSeats = totalNumberOfSeats exampleTrain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment