Keyed data, tables, absence and errors

So far the data has been plain arrays. Real data has labels, missing values and mistakes. You will learn:

  • the keyed flow ㅐ, which works through a list of keys;
  • tables: named columns, rows, sorting and filtering;
  • the grouped flow ㅔ, which works within each group;
  • absence, Garo's missing value, and how to deal with it;
  • what an error looks like, and how to catch one.

Keys

A key labels each element of some data. Two lists of the same length make keyed data: the first says which group each element is in, and the second holds the values.

댇 [`east, `west, `east] [20, 35, 40]
⇒ Table {key: [`east, `west], value: [60, 35]}Try it

댇 is reduce, keyed, sum. With the keyed flow ㅐ, the keys come first, then the data. The result is a table, named columns of the same length, with one row per key. In SQL this is SELECT key, SUM(value) GROUP BY key. The keys come out in the order each first appears.

Change the final and you change the aggregate, as before. 댐 counts, 댕 averages and 댁 finds the largest:

댕 [`east, `west, `east] [20, 35, 40]
⇒ Table {key: [`east, `west], value: [30.0, 35.0]}Try it

The keyed flow works in other families too. 냄 (select, keyed, cardinality) keeps the values whose key is one of p, like SQL's WHERE key IN (…):

냄 `east [`east, `west, `east] [20, 35, 40]
⇒ [20, 40]Try it

앰 (ref, keyed) looks one key up. The keys must be distinct here, since a lookup needs one answer:

앰 `b [`a, `b, `c] [10, 20, 30]
⇒ 20Try it

Tables

You write a table with Table and its columns in braces. Each column is an array, and every column has the same length. t.name gets a column out.

orders := Table {id: [1, 2, 3], amount: [5.0, 20.0, 12.5], status: [`pending, `shipped, `shipped]}
닫 orders.amount
⇒ 37.5Try it

A table works like a matrix with names. Its rows are along the first axis, so the flows mean what they mean on a matrix:

  • 엄 n picks row n, counting from 0. A single row comes back as a record: one value per column.
  • 남 n keeps the first n rows.
  • F나 keeps the rows for which F is true. Inside F, x is the row, so x.amount is that row's amount.
  • F가 applies F to each row.
orders := Table {id: [1, 2, 3], amount: [5.0, 20.0, 12.5], status: [`pending, `shipped, `shipped]}
{x.amount > 10}나 orders
⇒ Table {id: [2, 3], amount: [20.0, 12.5], status: [`shipped, `shipped]}Try it
orders := Table {id: [1, 2, 3], amount: [5.0, 20.0, 12.5], status: [`pending, `shipped, `shipped]}
엄 1 orders
⇒ Record {id: 2, amount: 20.0, status: `shipped}Try it

To sort the rows by a column, give that column as the keys. 색 sorts by key, greatest first (order, keyed, toward greater):

orders := Table {id: [1, 2, 3], amount: [5.0, 20.0, 12.5], status: [`pending, `shipped, `shipped]}
색 orders.amount orders
⇒ Table {id: [2, 3, 1], amount: [20.0, 12.5, 5.0], status: [`shipped, `shipped, `pending]}Try it

A keyed reduce of a table totals every column per key. 암 with a list of names picks those columns first:

orders := Table {id: [1, 2, 3], amount: [5.0, 20.0, 12.5], status: [`pending, `shipped, `shipped]}
댇 orders.status (암 [`amount] orders)
⇒ Table {key: [`pending, `shipped], amount: [5.0, 32.5]}Try it

Groups

The keyed flow does the grouping and the work in one step. You can also split the steps. 첮 (group, by key) makes a partition: for each key, the positions of its rows.

첮 [`east, `west, `east] [20, 35, 40]
⇒ Partition {`east: [0, 2], `west: [1]}Try it

Indexing the values by the partition gives groups: the values themselves, per key.

p := 첮 [`east, `west, `east] [20, 35, 40]
[20, 35, 40][p]
⇒ Groups {`east: [20, 40], `west: [35]}Try it

The grouped flow ㅔ works within each group. 덷 is reduce, grouped, sum:

p := 첮 [`east, `west, `east] [20, 35, 40]
덷 [20, 35, 40][p]
⇒ Table {key: [`east, `west], value: [60, 35]}Try it

Some results have one value per row instead of one per group. 렏 is the running total within each group, and each total goes back in its row's place:

p := 첮 [`east, `west, `east] [20, 35, 40]
렏 [20, 35, 40][p]
⇒ [20, 35, 60]Try it

Absence

Data has holes. Garo writes a missing value as `none and calls it absent. A value that is not absent is present. In SQL the same idea is NULL, and in pandas it is NaN.

Absence spreads. Arithmetic with an absent value gives an absent value:

[1, `none, 3] + 10
⇒ [11, `none, 13]Try it

So does an aggregate. The sum of a list with a hole in it is not known, so it is absent:

닫 [1, `none, 3]
⇒ `noneTry it

SQL would quietly skip the hole. Garo makes you choose, and the choice is one visible syllable:

  • 닢 (select, the absence final ㅍ) keeps only the present values.
  • 깊 p (map, the absence final ㅍ) fills each absent value with p.
닫 닢 [1, `none, 3]
⇒ 4Try it
깊 0 [1, `none, 3]
⇒ [1, 0, 3]Try it

The difference matters for a mean. Skipping the hole averages two numbers, and filling it with 0 averages three:

당 닢 [1.0, `none, 3.0]
⇒ 2.0Try it
당 깊 0 [1.0, `none, 3.0]
⇒ 1.3333333333333333Try it

Absence is also how Garo answers a question with no answer. A position past the end is absent, not an error:

엄 5 [10, 20]
⇒ `noneTry it

Errors

When a program cannot run, Garo prints an error. It starts with ! and the error's kind, then says what went wrong.

1 ÷ 0
⇒ !domain: division by zero (§17.3)Try it

Here the kind is domain: the operation was given a value it is not defined for. The other common kind is type, when the values are the wrong kind for the operation. Labels cannot be added:

닫 [`a, `b]
⇒ !type: sum needs numbers, texts or durations; Sym has none (§5.3, D45)Try it

The errors reference lists every kind.

A whole-array operation fails if any one element fails. To keep going, wrap the function in ?, which Garo calls Try. Instead of failing, ?F gives `ok and the result, or `err and the error's kind:

(?{1 ÷ x})기 [1, 0, 4]
⇒ [`ok 1.0, `err `domain, `ok 0.25]Try it

Each element's failure is now data you can count, filter or fill. A keyed-arm literal takes such a result apart. It is a block of key ⇒ result arms, one per case:

{ `ok v ⇒ v, `err k ⇒ 0 }기 ((?{1 ÷ x})기 [1, 0, 4])
⇒ [1.0, 0.0, 0.25]Try it

The same construct is Garo's only if: a Bool's two cases are `true and `false.

{ `true ⇒ "big", `false ⇒ "small" }기 ([3, 12, 7] > 5)
⇒ ["small", "big", "big"]Try it

What you have learned

  • With the keyed flow ㅐ, the keys come first, then the data.
  • A table is named columns. Its rows are its first axis.
  • The grouped flow ㅔ works within each group of a partition.
  • `none is an absent value. It spreads, and 닢 or 깊 says what to do with it.
  • An error has a kind. ? turns errors into values.

That is the whole tutorial. The guides put it to work: start with statistics on a small dataset.