Documentation contents
Functions and control flow
Summarize a week of rainfall readings with functions, conditions, loops over collections and functions passed as values.
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.
Functions
A function names its parameters and their types, gives a result type, and has an expression as its body. Rainfall is measured in millimetres throughout this module, so the types say so. A plain factor is marked dimensionless with empty brackets.
fn double(x: f64<mm>) -> f64<mm> = 2.0<> * x
When the body needs intermediate steps, use a block. Each let ends with a semicolon, and the last expression is the result. The names inside need no type annotations: the compiler infers them.
fn mean(xs: array<f64<mm>>) -> f64<mm> = {
let sum = total(xs);
let days = array.length(xs);
sum / days
}
Conditions
if is an expression, so it produces a value. Both branches produce the same type.
fn describe(level: f64<mm>) -> text =
if level >= 30.0<mm> then "heavy" else if level >= 10.0<mm> then "moderate" else "light"
Working through a collection
fold carries a running value through a collection. It names the running value and its starting point, then says how each element updates it.
fn total(xs: array<f64<mm>>) -> f64<mm> =
fold acc = 0.0<mm> with x in xs -> acc + x
for turns each element into a new one and returns the results as an array.
fn scaled(xs: array<f64<mm>>, factor: f64<>) -> array<f64<mm>> =
for x in xs -> factor * x
Inside square brackets, for can also filter. This keeps only the readings above a limit.
fn above(xs: array<f64<mm>>, limit: f64<mm>) -> array<f64<mm>> =
[for x in xs if x > limit -> x]
Applied to a week of readings:
node Rain = [4.0<mm>, 12.5<mm>, 31.0<mm>, 0.5<mm>]
node RainTotal = total(Rain)
node RainMean = mean(Rain)
node MeanLabel = describe(RainMean)
node Halved = scaled(Rain, 0.5<>)
node WetDays = above(Rain, 10.0<mm>)
node WetDayCount = array.length(WetDays)
Functions as values
A function can take another function as a parameter. The parameter's type is written as the argument types, an arrow and the result type.
fn applyTwice(f: (f64<mm>) -> f64<mm>, x: f64<mm>) -> f64<mm> = f(f(x))
An unnamed function is written with fn, its parameters and an arrow.
node Bumped = applyTwice(fn (x: f64<mm>) -> x + 1.5<mm>, 10.0<mm>)
The pipeline operator passes a value through a chain of one-argument functions, left to right.
node DoubledTotal = Rain |> total |> double
Generic functions
A type parameter in angle brackets lets one function work for many types. A bound after the colon says what the type must support. Here Ord means the values can be put in order.
fn largest<T: Ord>(a: T, b: T) -> T =
if a >= b then a else b
node Busiest = largest(3, 7)
node LaterName = largest("harbor", "ridge")
The floating-point types do not satisfy Ord, so largest accepts integers and text but not an f64. Comparison operators such as >= still work on floating-point values directly, as describe shows. Traits, generics and modules covers bounds in more detail.
Names that change
let gives a name to a value once. Two other bindings allow change inside a block, and neither affects code outside it.
var lets a name be given a new value.
fn corrected(reading: f64<mm>) -> f64<mm> = {
var value = reading;
value = 2.0<> * value;
value = value + 0.5<mm>;
value
}
mut makes storage that can be edited in place, such as an array that grows.
fn paired(a: f64<mm>, b: f64<mm>) -> array<f64<mm>> = {
mut items = ([] as array<f64<mm>>);
array.push(items, a);
array.push(items, b);
items
}
Use the weakest binding that does the job. The language tour compares the three.
Run the example
The complete module is station-flow.myc, run by the test manifest. The next page covers text and operators.