module Station.Library from Myceta.Collections.Map import * as Map from Myceta.Collections.Set import * as Set dimension Length quantity Rainfall = Length unit m: Length unit mm: Rainfall unit cm: Rainfall = 10 * mm fn checked(level: f64) -> Result, text> = if level < 0.0 then #Result.Err("negative rainfall") else #Result.Ok(level) fn doubled(level: f64) -> f64 = 2.0<> * level // Results node AllGood = Result.all([checked(4.0), checked(12.5)]) node AllGoodOk = AllGood.isOk() node OneBad = Result.all([checked(4.0), checked(-1.0)]) node OneBadErr = OneBad.isErr() node Checked = Result.collect([4.0, 12.5], checked) node CheckedOk = Checked.isOk() node Doubled = checked(4.0).map(doubled) node DoubledOk = Doubled.isOk() // Maps node Elevation = { let m0 = Map.empty>(); let m1 = Map.insert(m0, "Harbor", 3.0); Map.insert(m1, "Ridge", 410.0) } node StationCount = Map.len(Elevation) node KnowsRidge = Map.containsKey(Elevation, "Ridge") node WithoutHarbor = Map.remove(Elevation, "Harbor") node RemainingCount = Map.len(WithoutHarbor) node KnowsHarbor = Map.containsKey(WithoutHarbor, "Harbor") // Optional values node Calibration = Map.insert(Map.empty>(), "Ridge", 0.97<>) node RidgeFactor = Map.get(Calibration, "Ridge") node HasRidgeFactor = RidgeFactor.isSome() node HarborFactor = Map.get(Calibration, "Harbor").unwrapOr(1.0<>) node ValleyElevation = Map.get(Elevation, "Valley").okOr("not surveyed") node ValleySurveyed = ValleyElevation.isOk() // Ordering node Order = 4.cmp(9) node OrderText = match Order { Less -> "before" Equal -> "same" Greater -> "after" } // Text and characters node Parts = text.split("Harbor,Ridge,Valley", ",") node PartCount = array.length(Parts) node Middle = Parts[1] node Joined = text.join(Parts, " / ") node Shouted = text.to_upper("ridge") node Quiet = text.to_lower("RIDGE") node Swapped = text.replace("North Ridge", "North", "South") node Length = text.len("Valley") node Initial = text.charAt("Harbor", 0) node InitialCode = char.code(Initial) node InitialIsLetter = char.isLetter(Initial) node InitialText = char.toText(Initial) // Numbers node Readings = 7 node AsDecimal = i64.to_f64(Readings) / 2.0 node Truncated = f64.to_i64(3.9) node Root = f64.sqrt(2.25) node Power = f64.pow(2.0, 10.0) node CountText = i64.to_text(Readings) // Units node InCentimetres = convert>(25.0) node Bare = strip_unit(25.0) node Measured = attach_unit>(25.0<>) // Sets node Coastal = Set.insert(Set.insert(Set.empty(), "Harbor"), "Delta") node Windy = Set.insert(Set.insert(Set.empty(), "Harbor"), "Ridge") node Either = Set.len(Set.union(Coastal, Windy)) node Both = Set.len(Set.intersection(Coastal, Windy)) node OnlyCoastal = Set.contains(Set.difference(Coastal, Windy), "Delta") test LibraryBehaves { expect AllGoodOk : OK with value = true expect OneBadErr : OK with value = true expect CheckedOk : OK with value = true expect DoubledOk : OK with value = true expect StationCount : OK with value = 2 expect KnowsRidge : OK with value = true expect RemainingCount : OK with value = 1 expect KnowsHarbor : OK with value = false expect HasRidgeFactor : OK with value = true expect HarborFactor : OK with value = 1.0<> within 0.001<> expect ValleySurveyed : OK with value = false expect OrderText : OK with value = "before" expect PartCount : OK with value = 3 expect Middle : OK with value = "Ridge" expect Joined : OK with value = "Harbor / Ridge / Valley" expect Shouted : OK with value = "RIDGE" expect Quiet : OK with value = "ridge" expect Swapped : OK with value = "South Ridge" expect Length : OK with value = 6 expect InitialCode : OK with value = 72 expect InitialIsLetter : OK with value = true expect InitialText : OK with value = "H" expect AsDecimal : OK with value = 3.5 within 0.001 expect Truncated : OK with value = 3 expect Root : OK with value = 1.5 within 0.001 expect Power : OK with value = 1024.0 within 0.001 expect CountText : OK with value = "7" expect InCentimetres : OK with value = 2.5 within 0.001 expect Bare : OK with value = 25.0<> within 0.001<> expect Measured : OK with value = 25.0 within 0.001 expect Either : OK with value = 3 expect Both : OK with value = 1 expect OnlyCoastal : OK with value = true }