module Station.Text dimension Length quantity Rainfall = Length unit mm: Rainfall type Tally = { readings: i64; rainfall: f64 } impl Add for Tally { type Output = Tally fn add(self: Self, rhs: Self) -> Self::Output = Tally { readings = self.readings + rhs.readings; rainfall = self.rainfall + rhs.rainfall } } fn label(station: text, readings: i64) -> text = "{station}: {readings} readings" fn heading(station: text) -> text = text.to_upper(station) ++ " STATION" fn row(cells: array) -> text = text.join(cells, "\t") fn braces(name: text) -> text = "\{" ++ name ++ "\}" fn normalized(name: text) -> text = text.replace(text.to_lower(name), " ", "-") fn square(x: f64<>) -> f64<> = x ^ 2 fn stormy(rain: f64, gusts: bool) -> bool = rain >= 30.0 && gusts || rain >= 60.0 fn calm(rain: f64, gusts: bool) -> bool = not stormy(rain, gusts) fn chosen(preferred: optional) -> text = preferred ?? "Harbor" fn addReading(t: Tally, rain: f64) -> Tally = t with { .readings = t.readings + 1, .rainfall = t.rainfall + rain } fn total(t: Tally) -> f64 = t.rainfall node Label = label("Harbor", 3) node Heading = heading("Ridge") node HeadingLength = text.len(Heading) node Row = row(["Harbor", "34.5", "12.0"]) node Braces = braces("station") node Slug = normalized("North Ridge") node Squared = square(1.5<>) node HarborStormy = stormy(34.5, true) node RidgeCalm = calm(8.0, true) node Preferred = chosen(some("Ridge")) node Fallback = chosen(none) node Morning = Tally { readings = 2; rainfall = 12.5 } node Evening = Tally { readings = 3; rainfall = 22.0 } node Day = Morning + Evening node DayReadings = Day.readings node Updated = addReading(Day, 4.0) |> total test TextBehaves { expect Label : OK with value = "Harbor: 3 readings" expect Heading : OK with value = "RIDGE STATION" expect HeadingLength : OK with value = 13 expect Row : OK with value = "Harbor\t34.5\t12.0" expect Braces : OK with value = "\{station\}" expect Slug : OK with value = "north-ridge" expect Squared : OK with value = 2.25<> within 0.001<> expect HarborStormy : OK with value = true expect RidgeCalm : OK with value = true expect Preferred : OK with value = "Ridge" expect Fallback : OK with value = "Harbor" expect DayReadings : OK with value = 5 expect Updated : OK with value = 38.5 within 0.001 }