mighty

The mighty programming language, compiler and tools (WIP)
Log | Files | Refs

language2.mty (732B)


      1 ns app
      2 
      3 use io
      4 use math
      5 
      6 struct Vec2 ::
      7     float x
      8     float y
      9 
     10     float len() where result >= 0.0 =
     11         math.sqrt(.x ^ 2 + .y ^ 2)
     12 
     13     int clamp(int x, int lo, int hi)
     14         where lo <= hi
     15         where result >= lo and result <= hi
     16         =
     17         min(max(x, lo), hi)
     18 
     19 struct Reading ::
     20     int sensor
     21     float value
     22     bool valid
     23 
     24     bool usable() =
     25         .valid and .value >= 0.0
     26 
     27     str label() =
     28         "high" if .value > 100.0 else "normal"
     29 
     30 pre #xs > 0
     31 float mean(float[] xs) = +/xs / #xs
     32 
     33 
     34 float[] values(Reading[] rows) =
     35     rows[.usable()]
     36     | @(.value)
     37 
     38 
     39 int main() ::
     40     Reading[] rows = load_readings("readings.csv")
     41     float[] xs = values(rows)
     42 
     43     io.put("mean = " + str(mean(xs)))
     44     ret 0