
Keep
Block (Teacher's Choice)
The block takes a predicate (with a blank input slot) and a list as input and creates a new list keeping only those items from the input list that match some criterion, such as "begins with a vowel," "is less than five characters long," "is even," or "is a vegetable." For example:
You write the predicate that does the checking, and keep
applies that predicate to each item in the input list and reports the list of elements that make your predicate true.
keep
does in this script: keep
to create a filter that keeps only the values of a list that are over some input number. (It should be a reporter that takes a list and a number as input and reports a new list of all of the values in the original list that are greater than the specified number.)
Click on the script to open a new file.
true
or false
.Alphie and Betty discuss using keep
:
keep
works. Which states do we want to keep?states
. And letter 1 of the word needs to be the same as the last letter of the word.states
goes in the empty spaces. So, let's try an example. I'll pick a state... "new york". Letter 1 of "new york" is "n".length of
"new york" is 8, and letter 8 of
"new york" is "k". The condition is false because "n" doesn't equal "k". So "new york" doesn't show up in the list. We'll keep only the states that have the same first and last letter.Map
and Keep
to Access a DatabaseA database is a list of records. Each record is a list of its own (the "data" for that record). For example, a database of people in New York might consist of records that contain first name, last name, address, and phone number. To be useful, a database needs selectors that pull out individual pieces of information from each record.
map
and keep
might be useful in selecting and organizing data from a database.firstname
, lastname
, age
, and height
. Here's how age
might work:
Use this list of words and numbers:
Which of these expressions produce the list with just the numbers: {5, 34, 2, 305, 61}?
Choose all that apply.
Is a number?
block is a predicate, so map
will report a list of Boolean (true/false) values, not a list of numbers.