Students learn how to chain Methods together, and define more sophisticated subsets.
Lesson Goals |
Students will be able to…
|
|||||||||
Student-facing Lesson Goals |
|
|||||||||
Materials |
||||||||||
Preparation |
|
|||||||||
Language Table |
|
🔗Design Recipe Practice 25 minutes
Overview
Students practice more of what they learned in the previous lesson, applying the Design Recipe to make table functions that operate on rows of the Animals Dataset. These become the basis of the chaining activity that follows.
Launch
The Design Recipe is a powerful tool for solving problems by writing functions. It’s important for this to be like second nature, so let’s get some more practice using it!
Investigate
Define the Compute functions on The Design Recipe (Page 9) and The Design Recipe (Page 10).
Optional: Combining Booleans Suppose we want to build a table of Animals that are fixed and old, or a table of animals that are cats or dogs? By using the For many of the situations where you might use |
Synthesize
Did students find themselves getting faster at using the Design Recipe? Can students share any patterns they noticed, or shortcuts they used?
🔗Chaining 25 minutes
Overview
Students learn how to compose multiple table operations (sorting, filtering, building) on the same table - a technique called "chaining".
Launch
Now that we are doing more sophisticated analyses, we might find ourselves writing the following code:
# get a table with the nametags of all the fixed animals, ordered by species
with-labels = animals-table.build-column("labels", nametag)
fixed-with-labels = with-nametags.filter(is-fixed)
result = fixed-with-labels.order-by("species", true)
That’s a lot of code, and it also requires us to come up with names for each intermediate step! Pyret allows table methods to be chained together, so that we can build, filter and order a Table in one shot. For example:
# get a table with the nametags of all the fixed animals, ordered by species
result = animals-table.build-column("labels", nametag).filter(is-fixed).order-by("species", true)
This code takes the animals-table
, and builds a new column. According to our Contracts Page, .build-column
produces a new Table, and that’s the Table whose .filter
method we use. That method produces yet another Table, and we call that Table’s order-by
method. The Table that comes back from that is our final result.
Teaching Tip Use different color markers to draw nested boxes around each part of the expression, showing where each Table came from. |
It can be difficult to read code that has lots of method calls chained together, so we can add a line-break before each “.
” to make it more readable. Here’s the exact same code, written with each method on its own line:
# get a table with the nametags of all the fixed animals, order by species
animals-table
.build-column("label", nametag)
.filter(is-fixed)
.order-by("species", true)
Order matters: Build, Filter, Order.
Suppose we want to build a column and then use it to filter our table. If we use the methods in the wrong order (trying to filter by a column that doesn’t exist yet), we might wind up crashing the program. Even worse, the program might work, but produce results that are incorrect!
Let’s go through how we might meet the journalist’s needs.
-
Give the weight in kilograms.
For this step, we’ll use Build Attribute
, which makes a new copy of the dataset, adding a Weight (kgs)
column. (Hint: To get from pounds to kilograms, divide by 2.205.)
-
Include only fixed animals.
Here, we are going to apply the Filter
transformer not to our original Animals Dataset, but to the transformed copy of the original dataset. When we select Dataset to Filter
, we must choose BuildAttribute(Animals-Dataset)
. We will then specify that we want to keep all rows that satisfy Fixed = “TRUE”
.
-
Include only animals that weigh less than 9 kilograms.
We are going to Filter
again, but this time we apply the transformer to the dataset we created in the previous step. In the dropdown menu of datasets, our dataset is named Filter{BuildAttribute(Animals-Dataset)
.
-
Order weight in kilograms from lightest to heaviest.
Remember - we want to be able to easily create a table that meets the journalist’s specifications, so we’ll want to use the Sort
transformer here, rather than the Sort
dropdown option that appears when we click Weight
. Be sure to select the correct dataset to sort!
-
Update the table weekly to reflect the changing population of the shelter.
Because we’re using transformers, each time the Animals Table is updated, the journalist’s table will be updated, too! Try adding or removing a row from the original table and observe the ripple effect in your chained tables.
Investigate
When chaining methods , it’s important to build first, then filter, and then order.
How well do you know your table methods? Complete Chaining Methods (Page 11) and Chaining Methods 2: Order Matters (Page 12) in your Student Workbook to find out.
Synthesize
As our analysis gets more complex, chaining methods is a great way to re-use work we’ve already done. And less duplicate work means a smaller chance of bugs. Composing operations is a powerful way to work, so it’s critical to think carefully when we use it!
🔗Additional Exercises
These materials were developed partly through support of the National Science Foundation,
(awards 1042210, 1535276, 1648684, and 1738598).
Bootstrap:Data Science by the Bootstrap Community is licensed under a Creative Commons 4.0 Unported License. This license does not grant permission to run training or professional development. Offering training or professional development with materials substantially derived from Bootstrap must be approved in writing by a Bootstrap Director. Permissions beyond the scope of this license, such as to run training, may be available by contacting contact@BootstrapWorld.org.