Students learn about functions that work with tables, allowing them to filter and build columns
Lesson Goals 
Students will be able to…

Studentfacing Lesson Goals 

Materials 

Supplemental Materials 

🔗Review 15 minutes
Overview
Students practice carefully reading the contents of a starter file to review and gain comfort with rowconsuming functions.
Launch

Load the Table Functions Starter File, go to the File menu, and click "Save a Copy".

Hit "Run," then type
shapestable
in the Interactions Area. 
The way this file defines a table is different from what we’ve encountered before. How is it different?

Pyret also allows us to define Tables in code, rather than using a spreadsheet. This file defines the
shapestable
one row at a time.

Investigate
With your partner, take 5 minutes to complete Reading Row and Function Definitions.
Once students have completed the activity, follow up to confirm understanding.

What does then
isred
function do?
It looks up the color of the row, and checks if it is equal to the string
"red"
.


Who can explain what
ispolygon
does?
It looks up the value in the
polygon
column.


Who can explain what
istriangle
does?
It checks if the value in the
name
column contains"triangle"
.

Challenge students to use terminology like "looks up the value in the X column" when describing a lookup.
Synthesize

Were you able to guess what the functions did before testing them out? What was your strategy for making an educated guess?

What is the relationship between a row definition and a rowconsuming function?
🔗Filtering Rows, Building Columns 35 minutes
Overview
Students learn to filter the rows and add new columns to a table, using functions they can define themselves.
Launch
We need 68 students to line up shouldertoshoulder at the front of the room. Each will represent a “row” in a table of students.
Sometimes we want to filter the rows of a table by some criteria. In our animals dataset, for example, we might want to filter the table to show only the rows for dogs, or only the rows for cats older than 5. What are some ways to filter our table of students?

We need one volunteer to be
filter
, and another to be their helper function. 
The helper chooses one Function Card. They’ll read it carefully, making sure they understand what it will do for each "Row" (student!), but keeping it a secret.
(Choose your students and cards carefully! Some cards only work if you have at least one student with glasses, red hair, etc.)

filter
walks from student to student, pointing at each and asking “keep ‘em?”. 
The helper will answer with whatever the function on the card would produce for that student:
true
orfalse
. 
If the helper replies "true",
filter
asks that student to take a step forward. 
If the helper says "false",
filter
asks them to take a step back.
Here’s how that might look if filter
chose a function card hasglasses
.

filter
stands in front of Student 1. “Keep ‘em?” 
The helper checks if they are wearing glasses, and says “true”

filter
to Student 1 (who wears glasses): Step forward. (Student 1 steps forward.) 
filter
stands in front of Student 2. “Keep ‘em?” 
The helper checks if they are wearing glasses, and says “false”

filter
to Student 2 (who does not wear glasses): Step back. (Student 2 steps back.)
Have your helper function and filter
work together to act out one round of this. Ask the filter
if they can guess what’s on the helper function’s card, then ask the rest of the class. Give a different card to a different helper, and repeat the exercise.
Investigate
Pyret Tables have their own functions for filtering tables  and more! Let’s explore some of these.
Take 10 minutes to complete Exploring Table Functions
Key ideas from the activity are as follows:

filter
consumes a Table and a function that consumes Rows and produces Booleans. It produces a new table containing only rows for which the function returnstrue
. 
buildcolumn
consumes a Table, the name of a new column, and a function that consumes Rows. It produces a new table with that new column, and fills in the values by applying the function to every Row.
Let’s think about what filtering and building would look like with Animals at the shelter.

Match the code on What Table Do We Get? with the descriptions of the tables they would produce.
Give students some scaffolded practice filtering and building with the Animals!

Practice using the Design Recipe to write new helper functions:

Practice using Circles of Evaluation with Filtering and Building to make:

Then turn to Putting it All Together to practice defining new tables, defining new helper functions, and building displays in the Putting it All Together Starter File.
Common Misconceptions

Students often think that these functions change the table! In Pyret, all table functions produce a brand new table. If we want to save that table and use it later, we need to define it. For example:
dogs = filter(animalstable, isdog)
. 
Students might mistakenly think we need the
species
column to answer the question, "Which cat is the heaviest?". While we do look at the species column to filter the table, we’re not thinking about columns until after we’ve built a new table of just the cat rows, at which pointspecies
would be "cat" for all of the rows that we’re working with! 
Questions like "Who is the oldest?" or "What is the most?" require looking at every row in the table.
Synthesize
Using Table Functions is a huge upgrade in our ability to analyze data!

If the shelter is purchasing food for older cats, what filter would we write to determine how many cats to buy for?

A dataset from Europe might list everything in metric (centimeters, kilograms, etc), so we could build a column to convert that to imperial units (inches, pounds, etc).

A dataset about sports teams might include columns for how many games each team won and how many they lost, but it’s more useful to build a column to see what percentage of games those teams won.

What columns might you want to add to your dataset?

What filters might be interesting to apply to your dataset?
Project Option: Stress or Chill?
Students can gather data about their own lives, and use what they’ve learned in the class so far to analyze it. Optional Project: Stress or Chill? [rubric] can be used as a midterm or formative assessment, or as a capstone for a limited implementation of Bootstrap:Data Science.
These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, 1738598, 2031479, and 1501927). Bootstrap 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.