module Station.Records dimension Length dimension Time quantity Rainfall = Length quantity RainRate = Rainfall / Time unit h: Time unit mm: Rainfall unit mmph: RainRate = mm / h type Span = { low: T; high: T } type Storm = { station: text; rate: f64; duration: f64; derived total: f64 = rate * duration } type Gauge = { station: text; serial: text } fn longer(storm: Storm, extra: f64) -> Storm = storm with { .duration = storm.duration + extra } fn serialOf(gauge: optional) -> optional = match gauge { some(g) -> some(g.serial) none -> none } fn range(readings: (f64, f64)) -> f64 = { var (first, second) = readings; if first > second then first - second else second - first } fn ends(span: Span>) -> f64 = let (low, high) = (span.low, span.high) in high - low node Expected = Span { low = 5.0; high = 20.0 } node Codes = Span { low = "A"; high = "F" } node FirstCode = Codes.low node ExpectedWidth = ends(Expected) node Squall = Storm { station = "Harbor"; rate = 12.0; duration = 1.5 } node SquallTotal = Squall.total node Lingering = longer(Squall, 1.0) node LingeringTotal = Lingering.total node Summary = {| station = "Ridge"; days = 3; wettest = 31.0 |} node SummaryDays = Summary.days node Installed = some(Gauge { station = "Harbor"; serial = "H-7" }) node InstalledSerial = serialOf(Installed) node MissingSerial = serialOf(none) node InstalledLabel = InstalledSerial ?? "no gauge" node MissingLabel = MissingSerial ?? "no gauge" node Range = range((4.0, 12.5)) test RecordsBehave { expect FirstCode : OK with value = "A" expect ExpectedWidth : OK with value = 15.0 within 0.001 expect SquallTotal : OK with value = 18.0 within 0.001 expect LingeringTotal : OK with value = 30.0 within 0.001 expect SummaryDays : OK with value = 3 expect InstalledLabel : OK with value = "H-7" expect MissingLabel : OK with value = "no gauge" expect Range : OK with value = 8.5 within 0.001 }