Class summary:   How Tables Evaluate
1 Summary of Table Operations So Far
2 Plotting Table Data

Class summary: How Tables Evaluate

Copyright (c) 2017 Kathi Fisler

We began with a brief review of the tables functions that we saw in the previous lecture.

  gradebook = table: name, SNC, exam1, exam2

    row: "Allie", false, 85, 90

    row: "Carl",  false, 75, 60

    row: "Elan", true, 95, 63

    row: "Lavon", false, 87, 88

    row: "Nunu", true, 70, 0

  end

We also introduced a new operation, build-column, which can be used to add a new column to a table. Here is an example of adding a column containing the average of the two exam grades:

  # add a column with the average of the exam grades

  fun exam-avg(r :: Row) -> Number:

    (r["exam1"] + r["exam2"]) / 2

  end

  

  build-column(gradebook, "avg", exam-avg)

Like filter-by, build-column takes a function as an input. This function computes the value that should be in the cell of the new column for the given row.

If you try out this code in the interactions window, then ask to see the gradebook again, the new "avg" column isn’t there. What happened? Table functions in Pyret do not modify the table they are operating over. They return a new table, leaving the old table intact. This is actually an extremely useful feature of programming, and we will explore this issue further in the coming lectures.

1 Summary of Table Operations So Far

As a summary, here are the functions we’ve been working with, summarized with their types (read :: as "has the type"). We show them here to reiterate that filter-by and build-column take functions as arguments, and that each of these functions takes a row as input.

  filter-by :: (t :: Table, (test :: Row->Boolean)) -> Table

  sort-by :: (t :: Table, col :: String, ascending :: Boolean) -> Table

  build-column :: (T :: Table, col :: String,

                   builder :: (Row -> Value)) -> Table

Why do the functions for building and filtering take entire rows, rather than just the columns we need? filter-by supports filters over any collection of columns. Pyret can’t guess which data a particular filtering might need, so it provides the entire row to all functions, leaving the functions to extract the data that they need and to ignore the rest.

2 Plotting Table Data

Finally, we saw how to use the plotting functions (described in our tables documentation) to create scatter-plots and other plots from table data. These functions are fairly straightforward to use – they generally take a table and the names of the column(s) to use to generate the plots.

Here are some examples of how to create plots:

  # create plot that contrasts students' grades on the two exams

  scatter-plot(gradebook-real, "exam1", "exam2")

  

  # add a linear-regression line to the scatterplot

  # (no worries if you don't know what a linear regression is yet)

  lr-plot(gradebook-real, "exam1", "exam2")

  

  # plot the exam1 grades as bars in decreasing order

  bar-chart(sort-by(gradebook-real, "exam1", false), "name", "exam1")

You’ll get more practice with these in labs and on the project.