Table Methods
@BootstrapWorld
#BootstrapCS
Reviewing Function Definitions
@BootstrapWorld
#BootstrapCS
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
Ordering Tables
@BootstrapWorld
#BootstrapCS
Ordering Tables
What questions do you have?
What do .row-n and .order-by have in common?
How are they different?
@BootstrapWorld
#BootstrapCS
Filtering Tables
We need volunteers!
One will be the filter method.
The others should stand spaced across the front of the room…
@BootstrapWorld
#BootstrapCS
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
Filtering Tables
@BootstrapWorld
#BootstrapCS
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
Filtering Tables
@BootstrapWorld
#BootstrapCS
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
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
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
Building Columns
When might we want to build a column?
Come up with another example for when you might want to build a column!
@BootstrapWorld
#BootstrapCS
Additional Activities
@BootstrapWorld
#BootstrapCS