ela

Tutorial, lesson 9 of 9

Packages and data

ela’s procs come in packages, each named before the dot: str.upper works on text, list.sum on a list, json.decode on JSON. Nothing is imported; every package is there from the first line.

json.decode gives the document as maps and lists, as lesson 7 read them: $doc["shop"], $doc["orders"]. That suits a value or two. For rows, json.table gives a table straight from the text: at = "orders" is the path to the list, and each key of the first row is a column, $orders.columns their names.

A table sorts and filters itself. table.sort takes a column by name and desc = true for the largest first; table.filter with where = {"paid": false} keeps the rows whose cells match. Each gives a view of the same rows, so the order holds through the filter and nothing is copied. for row in $unpaid gives each row as a map by the column names.

A table checks its data and its calls. Write "amount": "120" for Grace’s order and run it: json.table stops at row 1, since a column holds one type. Write by = "amont", and the error lists the columns there are. Write {"paid": "no"}, and it says the column holds bools.

text.pad fills text out to a column with spaces, so the amounts line up on the right, and text.number writes 1310 as 1,310.00.

What it prints
5 orders at TEA.EXAMPLE
columns: id, customer, region, amount, paid

order  customer      amount
1042   Grace         120.00
1045   Ken            64.00
1043   Linus           8.75
owed                 192.75