Skip to main content

Day 7 - Functions over rows

Skills: 1, 2

Pre-reading: 4.1.3, 4.1.4.1, 4.1.4.2

Intro (15 min)

  • Consider we have the following table:
    orders = table: time, amount
    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, amount
    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(o :: Row) -> Boolean:
    o["amount"] >= 5.0
    where:
    is-high-value(orders.row-n(2)) is true
    is-high-value(orders.row-n(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)

  • Design a function is-morning that checks if the "time" column in a row represents a morning. Note that < works for strings, using "lexicographic" ordering. Now produce a new table that is only morning orders.
  • 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.
  • Write code to extract the amount of the latest morning order.
  • Load the data from the following url (see notes from yesterday for review): https://pdi.run/f25-2000-photos.csv
  • Now filter rows that have the subject "Forest" to create a new table.
  • 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 transform large sets of data.
  • Next class we'll see how else we can transform tables!