On this page:
table?
table
columns
row
table-ref
table-rows-ref
table-columns-ref
7.3

18 Tables

 (require rebellion/table) package: rebellion

A table is a data structure made up of a collection of rows. Tables contain a list of column names — represented by keywords — and each row has a value for each column in the row’s table. This makes rows similar to records, with the constraint that all the row records in a table have the same keys. Tables maintain rows as lists, not sets, so the order of rows in a table is significant and duplicate rows are allowed.

procedure

(table? v)  boolean?

  v : any/c
A predicate for tables.

syntax

(table (columns column-kw ...) (row row-value ...) ...)

Constructs a table containing all of the given rows and whose columns are named by the given column-kws. Row values are given by-position and there must be exactly one value for each column-kw.

Example:
> (table (columns #:name #:population #:capital-city)
         (row "Argentina" 43800000 "Buenos Aires")
         (row "Greece" 10800000 "Athens")
         (row "Nigeria" 198600000 "Abuja")
         (row "Japan" 126400000 "Tokyo"))

(table (columns #:capital-city #:name #:population)

       (row "Buenos Aires" "Argentina" 43800000)

       (row "Athens" "Greece" 10800000)

       (row "Abuja" "Nigeria" 198600000)

       (row "Tokyo" "Japan" 126400000))

syntax

columns

syntax

row

Syntactic forms recognized by table. Both columns and row are meaningless on their own; they can only be used in a table expression.

procedure

(table-ref tab pos column)  any/c

  tab : table?
  pos : natural?
  column : keyword?
Returns the value for column in the row at position pos in tab.

Examples:
> (define countries
    (table (columns #:name #:population #:capital-city)
           (row "Argentina" 43800000 "Buenos Aires")
           (row "Greece" 10800000 "Athens")
           (row "Nigeria" 198600000 "Abuja")
           (row "Japan" 126400000 "Tokyo")))
> (table-ref countries 3 '#:name)

"Japan"

procedure

(table-rows-ref tab pos)  record?

  tab : table?
  pos : natural?
Returns the row in tab at position pos, as a record mapping column names to their values in the row.

Examples:
> (define countries
    (table (columns #:name #:population #:capital-city)
           (row "Argentina" 43800000 "Buenos Aires")
           (row "Greece" 10800000 "Athens")
           (row "Nigeria" 198600000 "Abuja")
           (row "Japan" 126400000 "Tokyo")))
> (table-rows-ref countries 2)

(record #:capital-city "Abuja" #:name "Nigeria" #:population 198600000)

procedure

(table-columns-ref tab column)  list?

  tab : table?
  column : keyword?
Returns a list of all the values for column in tab, with each element of the list corresponding to one row in tab.

Examples:
> (define countries
    (table (columns #:name #:population #:capital-city)
           (row "Argentina" 43800000 "Buenos Aires")
           (row "Greece" 10800000 "Athens")
           (row "Nigeria" 198600000 "Abuja")
           (row "Japan" 126400000 "Tokyo")))
> (table-columns-ref countries '#:capital-city)

'("Buenos Aires" "Athens" "Abuja" "Tokyo")