Skip to content

data.tables

rows of data under named columns, like a spreadsheet: filter rows, pick, change or add columns, sort

A table is a grid of rows under named columns, like a spreadsheet. Make one from a list of maps, where each map is a row and its keys are the column names.

Listing a directory, globbing files and reading CSV all give tables. List functions work on tables too, and see each row as a map.

Tags: collections

  • table a table from a list of maps, one map per row
  • where keep only the rows that pass the test f, which gets each row as a map
  • select keep only the named columns, in the order given
  • reject remove the named columns
  • update a new table with one column’s cells replaced by f(cell)
  • insert a new table with an added last column col, holding f(row) for each row, where the row is a map
  • sort_by the rows sorted by the values in a column, smallest first
  • sort_by_desc the rows sorted by the values in a column, largest first
1
# table
2
[{a: 1, b: 2}] to tabletable([{a: 1, b: 2}])
Example outputCtrl+Enter to run
1
# t.col: a column, as a list
2
[{a: 1}, {a: 2}].table.a[1, 2]
3
# t[i]: a row, as a map
4
[{a: 1}, {a: 2}].table[1]{a: 2}
Example outputCtrl+Enter to run
1
# long, the default: a grid
2
pretty([{a: 1234, b: "x"}].table)
# a b ──────────── 0 1,234 x ──────────── 1 row · 2 columns
3
# short: all on one line, as a list of maps
4
pretty([{a: 1234, b: "x"}].table, "short")"table([{a: 1.23K, b: \"x\"}])"
Example outputCtrl+Enter to run
table(rows: list|table) -> table

a table from a list of maps, one map per row

Every key that appears becomes a column, and rows without it get nil there

1
[{a: 1}, {a: 2, b: 3}].tabletable([{a: 1, b: nil}, {a: 2, b: 3}])
2
[{a: 1}] to tabletable([{a: 1}])
Example outputCtrl+Enter to run

See also select, update, list

where(t: table, f: fn) -> table

keep only the rows that pass the test f, which gets each row as a map

1
[{n: 1}, {n: 5}].table.where(|r| r.n > 2)table([{n: 5}])
Example outputCtrl+Enter to run

See also filter, sort_by

select(t: table, *cols: str) -> table

keep only the named columns, in the order given

1
[{a: 1, b: 2, c: 3}].table.select("c", "a")table([{c: 3, a: 1}])
Example outputCtrl+Enter to run

See also reject

reject(t: table, *cols: str) -> table

remove the named columns

1
[{a: 1, b: 2, c: 3}].table.reject("b")table([{a: 1, c: 3}])
Example outputCtrl+Enter to run

See also select, update

update(t: table, col: str, f: fn) -> table

a new table with one column’s cells replaced by f(cell)

If f takes two params it also gets the row as a map, so a cell can be worked out from the others

1
[{n: 1}, {n: 5}].table.update("n", _ * 10)table([{n: 10}, {n: 50}])
2
[{a: 2, b: 3}].table.update("b", |v, r| v * r.a)table([{a: 2, b: 6}])
Example outputCtrl+Enter to run

See also map, select, insert

insert(t: table, col: str, f: fn) -> table

a new table with an added last column col, holding f(row) for each row, where the row is a map

The column must not exist yet, use update to change one

1
[{a: 2, b: 3}].table.insert("total", |r| r.a + r.b)table([{a: 2, b: 3, total: 5}])
Example outputCtrl+Enter to run

See also update, select

sort_by(t: table, col: str) -> table

the rows sorted by the values in a column, smallest first

1
[{n: 3}, {n: 1}].table.sort_by("n")table([{n: 1}, {n: 3}])
Example outputCtrl+Enter to run

Tags: sorting

See also sort_by_desc, sort

sort_by_desc(t: table, col: str) -> table

the rows sorted by the values in a column, largest first

1
[{n: 1}, {n: 3}].table.sort_by_desc("n")table([{n: 3}, {n: 1}])
Example outputCtrl+Enter to run

Tags: sorting

See also sort_by

1
# a column as a list
2
[{n: 1}, {n: 5}].table.n[1, 5]
3
# list functions see each row as a map
4
[{n: 1}, {n: 5}].table.map(|r| r.n * 2)[2, 10]
5
# sort, then take one column
6
[{f: "a", kb: 3}, {f: "b", kb: 9}].table.sort_by_desc("kb").f["b", "a"]
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.