1 of 15

Table Methods

@BootstrapWorld

#BootstrapCS

2 of 15

Reviewing Function Definitions

  1. Make sure you are logged into CPO.
  2. Open the Table Methods Starter File.
  3. Click "Save a copy".
  4. Complete Reading Function Definitions (pg 27) in your student workbooks.

@BootstrapWorld

#BootstrapCS

3 of 15

Ordering Tables

Find the contract for .order-by in your contracts pages.

The .order-by method consumes a String (the name of the column by which we want to order) and a Boolean (true for ascending, false for descending).

But what does it produce?

@BootstrapWorld

#BootstrapCS

4 of 15

Ordering Tables

  1. Open your saved Table Methods Starter File, and click “Run”.
  2. Type animals-table.order-by("name", true) into the Interactions Area. What do you get?
  3. Type animals-table.order-by("age", true) into the Interactions Area. What do you get?
  4. Order the animals table from heaviest-to-lightest.
  5. Order the animals table alphabetically by species.
  6. Order the animals table by how long it took for each animal to be adopted, in ascending order.

@BootstrapWorld

#BootstrapCS

5 of 15

Ordering Tables

What questions do you have?

What do .row-n and .order-by have in common?

How are they different?

@BootstrapWorld

#BootstrapCS

6 of 15

Filtering Tables

We need volunteers!

One will be the filter method.

The others should stand spaced across the front of the room…

@BootstrapWorld

#BootstrapCS

7 of 15

Filtering Tables

Find the contract for .filter in your contracts pages.

The .filter method is taking in a function! What is the contract for that function?

The .filter method takes a function, and produces a new table containing only rows for which the function returns true.

<Table>.filter :: (test :: (Row ->Boolean)) -> Table

@BootstrapWorld

#BootstrapCS

8 of 15

Filtering Tables

  1. In the Interactions Area, type animals-table.filter(is-fixed). What did you get?
  2. What do you expect animals-table to produce now? Try it out. What happened?
  3. Try animals-table.filter(is-old). What did you get?
  4. Try animals-table.filter(is-dog). What did you get?
  5. Try animals-table.filter(lookup-name). What happened? Why do you think it happened?

@BootstrapWorld

#BootstrapCS

9 of 15

Filtering Tables

In Pyret, all table methods produce a brand new table.

If we want to save that table, we need to define it.

For example: cats = animals-table.filter(is-cat).

Does making new tables change the original table?

@BootstrapWorld

#BootstrapCS

10 of 15

Filtering Tables

  • Suppose we wanted to determine whether cats or dogs get adopted faster. How might using the .filter method help?
  • If the shelter is purchasing food for older cats, what filter would we write to determine how many cats to buy for?
  • Can you think of a situation where filtering fixed animals would be helpful?

@BootstrapWorld

#BootstrapCS

11 of 15

Building Columns

Suppose we want to transform our table, converting pounds to kilograms or weeks to days.

Suppose we want to add a "cute" column that just identifies the puppies and kittens.

Find the contract for <table>.build-column

@BootstrapWorld

#BootstrapCS

12 of 15

Building Columns

Type animals-table.build-column("old", is-old) into the Interactions Area.

Type animals-table.build-column("sticker", label) into the Interactions Area.

What do you get? What do you think is going on?

From the File menu, click “Save a Copy”.

@BootstrapWorld

#BootstrapCS

13 of 15

Building Columns

The .build-column method is taking in a function and applying that function to each row. Whatever it produces will be recorded in the new column specified by the string.

<Table>.build-column :: (col::Str, f::(Row->Any)) -> Table

@BootstrapWorld

#BootstrapCS

14 of 15

Building Columns

When might we want to build a column?

  • The animals shelter might want to print nametags for every animal. They could build a column using nametag function!
  • 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)
  • We might have columns for how many students a school has, and how many pass the state exam. But when comparing different-sized schools, what we really want is a column showing what percentage passed.

Come up with another example for when you might want to build a column!

@BootstrapWorld

#BootstrapCS

15 of 15

Additional Activities

  • What Table do we get?

@BootstrapWorld

#BootstrapCS