Examples

A package is also a folder a program can start in. This one tries the

The 4 files of examples/filters/, and what zeno run examples/filters/main.zeno prints.

filters/main.zeno
# A package is also a folder a program can start in. This one tries the
# package's own modules:
#
#     zeno run examples/filters/main.zeno
#
# Here `import lowpass` is the file `lowpass.zeno` beside this one. The
# program in examples/thermostat needs this package, and writes the same
# module as `import filters.lowpass`.
import filters
import lowpass

print(filters.clamp(12.5, 0.0, 10.0))
print(filters.mean([1.0, 2.0, 6.0]))

let filter = lowpass.start(0.5, 20.0)
for reading in [20.0, 24.0, 24.0, 24.0] {
    print(f"{filter.step(reading):.2f}")
}
filters/filters.zeno
# The module that has the package's own name. `import filters` gets it,
# from this package or from a program that needs the package.

# `value`, kept between `low` and `high`.
function clamp(value: float, low: float, high: float) -> float {
    if value < low {
        return low
    }
    if value > high {
        return high
    }
    return value
}

function mean(values: list[float]) -> float {
    var sum = 0.0
    for value in values {
        sum += value
    }
    return sum / len(values)
}
filters/lowpass.zeno
# The module `lowpass` of the package `filters`: a filter that smooths
# a noisy signal. A program that needs the package writes
# `import filters.lowpass`.
#
# Inside the package an import names a file of the package itself, so
# this line is `filters.zeno` in this folder, whichever program the
# package is being used by.
from filters import clamp

class Lowpass {
    weight: float       # 0.0 never moves, 1.0 follows the signal at once
    value: float        # the smoothed signal so far

    # Take one new reading in, and return the smoothed value.
    function step(self, reading: float) -> float {
        self.value += self.weight * (reading - self.value)
        return self.value
    }
}

# A filter that starts at `first`.
function start(weight: float, first: float) -> Lowpass {
    return Lowpass(clamp(weight, 0.0, 1.0), first)
}
zeno run examples/filters/main.zeno
10.0
3.0
20.00
22.00
23.00
23.50
filters/zeno.package
# This file makes the folder a package: a folder of modules that a
# program in another folder can use. The first line names it. A program
# that needs it says so in its own `zeno.package`:
#
#     needs filters in "../filters"
#
# and then imports its modules as `filters` and `filters.lowpass`.
package filters

See it on GitHub: examples/filters/.