Learn › Beginner · Lesson 8 of 11

Regions and aggregates

A region is a set of positions, written like a restriction: (t : 0 .. 9) is t from 0 to 9. An aggregate combines a value over a region: sum, mean, min, max and count, written sum e over R end.

-- Windows, totals and a 3 × 3 block.
reading = restrict 20 + mod(#t * 7, 5) to (t : 10) end
avg3 = mean reading over (t : #t - 2 .. #t) end
highest = max reading over (t : 0 .. 9) end
how_many_warm = count reading over (t : 0 .. 9) if reading > 22 end
grid = restrict 10 * #x + #y to (x : 10, y : 10) end
block = sum grid over (x : #x - 1 .. #x + 1, y : #y - 1 .. #y + 1) end
cells = count grid over (x : #x - 1 .. #x + 1, y : #y - 1 .. #y + 1) end
-- ask avg3[t = 4]
-- expect avg3[t = 0] = 20
-- expect avg3[t = 2] = 22
-- expect avg3[t = 4] = 22.6667
-- expect highest = 24
-- expect how_many_warm = 4
-- expect block[x = 5, y = 5] = 495
-- expect cells[x = 0, y = 0] = 4

Open in playground

How to read it:

Sums are exact. eLucid8 adds numbers as if exactly and rounds once at the end, so the order of a sum never changes its answer, and a mean shows 22.6667, never a long tail of rounding. product e over R end multiplies the same way: exactly, rounded once.

An aggregate stops at eod: if a reading in the region is eod, so is the result. Write skip eod after the region, mean reading over (t : #t - 2 .. #t) skip eod end, to leave such readings out instead.

Try it in the playground

  1. The answer is there as the program opens, with no button to press: avg3[t = 4] is 22.6667.
  2. Press Why?: the trace shows the three readings the mean read.
  3. Query cells at x = 0, y = 0 (4) and at x = 5, y = 5 (9).
  4. Change mean to max in avg3: the row redraws by itself, the highest of each three readings.

Ask your own AI

Using eLucid8's reference at https://elucid8-lang.org/llms.txt, write a Beginner eLucid8 program (equations only: no input or output lines) with made-up daily prices for 30 days and their 5-day moving average. Compile it with the elucid8 tools before you show it, and give me its Open in playground link.