The steps of the Design Recipe don’t change just because we’re working with Rows, but we can make some adjustments when using Row-consuming functions to filter tables and build columns! Let’s try a concrete example:
Write a function is-lizard
, which tells us whether an animal is a lizard.
Contract and Purpose
-
We still want to pick good names. Are we writing a function to check if an animal is a lizard? Call it
is-lizard
! -
The Domain is a lot easier — it’s always a
Row
! -
The Range is easier, too. If we’re writing a function to filter a Table, we know the Range has to be a Boolean. (What would it be if we were building a column of Numbers? Images? Strings?)
Examples
The goal of the Examples step is to find the pattern that represents what the function does. When working with Rows, sometimes we have to start by just focusing on what the answer should be.
Suppose animalC
is a lizard, and animalD
is a cat. We can imagine the answers for an is-lizard
to be…
examples:
is-lizard(animalC) is true
is-lizard(animalD) is false
end
But how do we know these are true
and false
? Well, we KNOW animalC
is a lizard, and we KNOW animalD
is a cat. So let’s replace those answers with the Boolean expressions that compare their species:
examples:
is-lizard(animalC) is "lizard" == "lizard" # will produce true
is-lizard(animalD) is "cat" == "lizard" # will produce false
end
But what work gives us "lizard" and "cat"? Well, we can look in the species
column!
examples:
is-lizard(animalC) is animalC["species"] == "lizard" # will produce true
is-lizard(animalD) is animalD["species"] == "lizard" # will produce false
end
Sometimes we can go straight to this final form, doing the whole thing in one step. But it’s nice to know we can break it down into pieces if we have to.
Once we see the pattern, we can circle and label what changes. In this case, only the Row representing the animal changes! We might use r
as label, to represent the Row.
Definition
The final step in the Design Recipe is to take the pattern from our examples and generalize it to work with any input. It’s no different when working with Rows.
Once again, our previous step is a huge help. We can simply copy everything that stays the same, and replace the part that changes with the label we used:
fun is-lizard(r): r["species"] == "lizard" end
These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, and 1738598). 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.