Documentation contents
Collections and missing values
Keep readings in arrays, look stations up in maps and sets, and handle a reading that is missing or invalid.
On this page
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.
Arrays
An array is an ordered sequence of values of one type. Positions start at zero.
node Rain = [4.0<mm>, 12.5<mm>, 31.0<mm>]
node WithToday = array.append(Rain, 0.5<mm>)
node DayCount = array.length(WithToday)
node FirstDay = Rain[0]
Arrays are values. array.append returns a new array and leaves Rain as it was. To edit an array in place inside a function, bind it with mut, as shown in functions and control flow.
Vectors and matrices
vector and matrix are the collections for numerical work. They are written with their name before the brackets.
node Wind = vector[3.0, 4.0]
node WindCount = vector.length(Wind)
node Grid = matrix[[1.0, 2.0], [3.0, 4.0]]
node GridRows = matrix.rows(Grid)
node Flipped = matrix.transpose(Grid)
for and fold work on vectors as they do on arrays.
Maps and sets
Maps and sets live in the standard library and are imported under a name of your choice.
from Myceta.Collections.Map import * as Map
from Myceta.Collections.Set import * as Set
A map pairs keys with values. Like an array, it is a value: insert returns a new map. Give the key and value types when creating an empty one.
node Elevation = {
let m0 = Map.empty<text, f64<m>>();
let m1 = Map.insert(m0, "Harbor", 3.0<m>);
Map.insert(m1, "Ridge", 410.0<m>)
}
node KnowsValley = Map.containsKey(Elevation, "Valley")
A set holds each member once and supports the usual set operations.
node Coastal = {
let s0 = Set.empty<text>();
Set.insert(s0, "Harbor")
}
node Reporting = {
let s0 = Set.empty<text>();
let s1 = Set.insert(s0, "Harbor");
Set.insert(s1, "Ridge")
}
node CoastalReporting = Set.intersection(Coastal, Reporting)
node HarborReports = Set.contains(CoastalReporting, "Harbor")
A value that may be missing
optional<T> is either some(value) or none. A function that might have nothing to return says so in its type.
fn latest(xs: array<f64<mm>>) -> optional<f64<mm>> =
if array.length(xs) == 0 then none else some(xs[array.length(xs) - 1])
The caller has to deal with both possibilities before using the value. match handles each one. Here the caller says what to use when there is no reading.
fn orElse(value: optional<f64<mm>>, fallback: f64<mm>) -> f64<mm> =
match value {
some(v) -> v
none -> fallback
}
Two shorter forms cover the common cases. ?? supplies a value to use in place of none, and let with else binds the value or falls back.
node Typical = 10.0<mm>
node Latest = latest(Rain)
node LatestOrTypical = orElse(Latest, Typical)
node NoReadings = latest(([] as array<f64<mm>>))
node Carried = NoReadings ?? Typical
node Guarded = let some(v) = Latest else Typical
Choose a fallback that means something in the problem, as the typical rainfall does here. Do not invent a number such as -1 to stand for "missing": keep the optional and let the code that knows what to do decide.
Looking up a key in a map returns an optional for the same reason: the key may not be there.
fn surveyNote(found: optional<f64<m>>) -> text =
match found {
some(_) -> "surveyed"
none -> "not surveyed"
}
node RidgeNote = surveyNote(Map.get(Elevation, "Ridge"))
node ValleyNote = surveyNote(Map.get(Elevation, "Valley"))
A result that may be an error
Result<T, E> is a union with two cases: Ok carries the answer and Err carries the reason there is none.
fn checked(level: f64<mm>) -> Result<f64<mm>, text> =
if level < 0.0<mm> then #Result.Err("negative rainfall") else #Result.Ok(level)
Because it is an ordinary union, match takes it apart, and one check can feed the next.
fn checkedSum(a: f64<mm>, b: f64<mm>) -> Result<f64<mm>, text> =
match checked(a) {
Err(reason) -> #Result.Err(reason)
Ok(first) ->
match checked(b) {
Err(reason) -> #Result.Err(reason)
Ok(second) -> #Result.Ok(first + second)
}
}
fn report(result: Result<f64<mm>, text>) -> text =
match result {
Ok(_) -> "accepted"
Err(reason) -> "rejected: " ++ reason
}
node GoodSum = report(checkedSum(4.0<mm>, 12.5<mm>))
node BadSum = report(checkedSum(4.0<mm>, -1.0<mm>))
A Result is data. A function that returns Err has still produced a value, and the node holding it finishes normally. Whether a node itself succeeded is a separate question, answered by its outcome.
Run the example
The complete module is station-collections.myc, run by the test manifest. The next page covers traits, generics and modules.