Skip to main content

Day 8 - Functions over rows

Date: September 24, 2026

⚠️Homework
Homework 2 due September 27, 2026

Skills: 0, 1, 2​

Pre-reading: 4.1.3, 4.1.4.1, 4.1.4.2​

Supplementary Videos​

Functions over Rows

Reference: For all work with tables, refer to the Tables page in the menu at the top of the page!​

Intro (15 mins)​

  • Consider we have the following table:
    orders = table: time :: String, amount :: Number
    row: "08:00", 10.50
    row: "09:30", 5.75
    row: "10:15", 8.00
    row: "11:00", 3.95
    row: "14:00", 4.95
    row: "16:45", 7.95
    end
  • Let's say we want to find orders that are "high-value" (e.g., amount >= 8).
  • To do this, we want to create a new table with rows that satisfy this. With a small table, we could do this manually, i.e.,:
    high-value-orders = table: time :: String, amount :: Number
    row: "08:00", 10.50
    row: "10:15", 8.00
    end
    But if we have hundreds or thousands of orders, this is impractical.
  • Better is to design a function that checks if an order is high-value, and then use a table operation, filter-with, to create a new table that contains only rows that satisfy the function.
    fun is-high-value(r :: Row) -> Boolean:
    doc: "returns whether the amount column is >= 5"
    get-column(r, "amount") >= 5.0
    where:
    is-high-value(get-row(orders, 2)) is true
    is-high-value(get-row(orders, 3)) is false
    end
  • Now we can use is-high-value with a table operation, filter-with to construct a new table:
    new-high-orders = filter-with(orders, is-high-value)
    This constructs the same table as we did by hand:
    check:
    new-high-orders is high-value-orders
    end
    But this approach will work on tables that have hundreds, thousands, or even millions of rows.
  • Another built in operation on tables is order-by. This takes a table, a column name, and a boolean to indicate if the rows should be ordered in ascending or descending order by the given column. We can use this to sort by amount:
    order-by(orders, "amount", true)

Class Exercise (40 mins)​

  1. Design a function is-morning that checks if the "time" column in a row represents a morning. Note that < works for strings, using "lexicographic" (i.e., character by character) ordering. Now produce a new table that is only morning orders.
  2. Create a table where the orders are sorted by time from latest to earliest, rather than how the original table is sorted. Use order-by, rather than constructing it by hand.
  3. Write code to extract the amount of the latest morning order.
  4. Load the data from the following url (see notes from yesterday for review): https://raw.githubusercontent.com/neu-pdi/cs2000-public-resources/refs/heads/main/static/support/7-photos.csv
  5. Now filter rows that have the subject "Forest" to create a new table.
  6. Order the resulting new table by date, and extract the location from the most recent row.

Wrap-up (5 mins)​

  • Operations on tables, like filter-with and order-by, allow us to easily use code to work with large sets of data by restricting to certain subsets and re-ordering them.
  • Next class we'll see how else we can transform tables!