module Station.Match dimension Length dimension Time quantity Rainfall = Length quantity Speed = Length / Time unit m: Length unit s: Time unit mm: Rainfall unit mps: Speed = m / s type Source = | Gauge(text) | Radar | Estimate type Sky = | Dry | Wet(f64) type Wind = | Still | Gusting(f64) fn sourceName(source: Source) -> text = match source { Gauge(name) -> "gauge at " ++ name Radar -> "radar" Estimate -> "estimate" } fn trusted(source: Source) -> bool = match source with | Gauge(_) -> true | Radar -> true | _ -> false fn rainBand(sky: Sky) -> text = match sky with | Wet(rain) when rain >= 30.0 -> "heavy" | Wet(rain) when rain >= 10.0 -> "moderate" | Wet(_) -> "light" | Dry -> "dry" fn outlook(sky: Sky, wind: Wind) -> text = match (sky, wind) { (Dry, Still) -> "calm" (Wet(rain), Gusting(speed)) when rain >= 30.0 && speed >= 20.0 -> "storm" (Wet(_), _) -> "rain" (Dry, Gusting(_)) -> "windy" } fn station(code: text) -> text = if code == "H" then "Harbor" else if code == "R" then "Ridge" else "unknown" node HarborSource = sourceName(#Source.Gauge("Harbor")) node RadarTrusted = trusted(#Source.Radar) node EstimateTrusted = trusted(#Source.Estimate) node HeavyBand = rainBand(#Sky.Wet(34.5)) node LightBand = rainBand(#Sky.Wet(2.0)) node DryBand = rainBand(#Sky.Dry) node Storm = outlook(#Sky.Wet(41.0), #Wind.Gusting(26.0)) node Shower = outlook(#Sky.Wet(41.0), #Wind.Gusting(8.0)) node Windy = outlook(#Sky.Dry, #Wind.Gusting(26.0)) node Calm = outlook(#Sky.Dry, #Wind.Still) node RidgeCode = station("R") test MatchingBehaves { expect HarborSource : OK with value = "gauge at Harbor" expect RadarTrusted : OK with value = true expect EstimateTrusted : OK with value = false expect HeavyBand : OK with value = "heavy" expect LightBand : OK with value = "light" expect DryBand : OK with value = "dry" expect Storm : OK with value = "storm" expect Shower : OK with value = "rain" expect Windy : OK with value = "windy" expect Calm : OK with value = "calm" expect RidgeCode : OK with value = "Ridge" }