Documentation contents
Standard library essentials
The everyday functions for results, optional values, ordering, text, characters, numbers, units, arrays, maps and sets. Every call on this page is exercised in station-library.myc.
Myceta 2.0. These pages describe the language as planned for 2.0, the first public release, which is still in development. Details may change before release. The examples were type-checked and tested with the development compiler.
Results
Result<T, E> is the union Ok(T) | Err(E). It is in scope in every module.
| Call | Result |
|---|---|
r.isOk(), r.isErr() | Which case r is. |
r.map(f) | Ok(f(v)) for Ok(v); an Err passes through unchanged. |
Result.all(results) | Ok with every value if all are Ok; otherwise the first Err. |
Result.collect(items, f) | Applies f to each item, then combines the results as Result.all does. |
fn checked(level: f64<mm>) -> Result<f64<mm>, text> =
if level < 0.0<mm> then #Result.Err("negative rainfall") else #Result.Ok(level)
node AllGood = Result.all([checked(4.0<mm>), checked(12.5<mm>)])
node OneBad = Result.all([checked(4.0<mm>), checked(-1.0<mm>)])
node Checked = Result.collect([4.0<mm>, 12.5<mm>], checked)
Optional values
| Call | Result |
|---|---|
o.isSome() | Whether a value is present. |
o.unwrapOr(d), o ?? d | The value, or d when there is none. |
o.okOr(e) | Ok with the value, or Err(e) when there is none. |
Choose a fallback that means something in the problem. Here a station with no calibration factor is left uncorrected, and a station with no survey is reported as such.
node Calibration = Map.insert(Map.empty<text, f64<>>(), "Ridge", 0.97<>)
node HarborFactor = Map.get(Calibration, "Harbor").unwrapOr(1.0<>)
node ValleyElevation = Map.get(Elevation, "Valley").okOr("not surveyed")
Ordering
a.cmp(b) returns an Ordering: Less, Equal or Greater. It works for any type that implements Ord.
node Order = 4.cmp(9)
node OrderText = match Order {
Less -> "before"
Equal -> "same"
Greater -> "after"
}
Text and characters
| Call | Result |
|---|---|
text.len(s) | Number of characters (Unicode scalar values). |
text.split(s, separator) | An array<text> of the pieces between separators. |
text.join(parts, separator) | The parts joined in order. An empty array gives empty text. |
text.replace(s, from, to) | s with each from replaced by to. |
text.to_upper(s), text.to_lower(s) | Case conversion. |
text.charAt(s, i) | The character at position i, counting from 0. |
char.code(c) | The character's code point, as an i64. |
char.isLetter(c) | Whether the character is a letter. |
char.toText(c) | A one-character text. |
node Parts = text.split("Harbor,Ridge,Valley", ",")
node Joined = text.join(Parts, " / ")
node Initial = text.charAt("Harbor", 0)
node InitialCode = char.code(Initial)
Joined is "Harbor / Ridge / Valley" and InitialCode is 72.
Numbers
| Call | Result |
|---|---|
i64.to_f64(n) | The integer as a floating-point number. |
f64.to_i64(x) | The whole-number part of x: 3.9 gives 3. |
i64.to_text(n) | The integer written as text. Interpolation, "{n}", does the same inside a literal. |
f64.sqrt(x) | Square root. |
f64.pow(x, y) | x to the power y, for any exponent. For a small whole-number exponent written in the source, x ^ 2 reads better. |
Units
| Call | Result |
|---|---|
convert<f64<U>>(x) | x in unit U, which must measure the same quantity. |
strip_unit(x) | The number without its unit, as f64<>. |
attach_unit<f64<U>>(x) | A unitless number given unit U. |
unit cm: Rainfall = 10 * mm
node InCentimetres = convert<f64<cm>>(25.0<mm>)
Use strip_unit and attach_unit only where numbers cross a boundary that has no units, such as reading a file. Units of measure explains when.
Arrays
| Call | Result |
|---|---|
array.length(xs) | Number of elements. |
array.append(xs, x) | A new array with x added at the end. |
xs[i] | The element at position i, counting from 0. |
Maps and sets
Import the collection modules under a short name.
from Myceta.Collections.Map import * as Map
from Myceta.Collections.Set import * as Set
Maps and sets are values: insert and remove return a new collection and leave the original as it was.
| Call | Result |
|---|---|
Map.empty<K, V>() | A map with no entries. |
Map.insert(m, k, v), Map.remove(m, k) | A new map with the entry added or replaced, or removed. |
Map.get(m, k) | An optional<V>: the value, or none when the key is absent. |
Map.containsKey(m, k), Map.len(m) | Whether a key is present; the number of entries. |
Set.empty<T>(), Set.insert(s, x) | A set with no elements; a new set with x added. |
Set.contains(s, x), Set.len(s) | Whether x is present; the number of elements. |
Set.union(a, b), Set.intersection(a, b), Set.difference(a, b) | Elements in either, in both, or in a but not b. |
node Coastal = Set.insert(Set.insert(Set.empty<text>(), "Harbor"), "Delta")
node Windy = Set.insert(Set.insert(Set.empty<text>(), "Harbor"), "Ridge")
node Either = Set.len(Set.union(Coastal, Windy))
node Both = Set.len(Set.intersection(Coastal, Windy))
Either is 3 and Both is 1.