Page 5: Transforming Every List Item

Unit 3, Lab 2, Page 5

On this page, you will use the map over list block to create an initials from name block and then use map over list together with initials from name to find the initials of all of your contacts.

Alphie, Betsy, and Gamal are building an initials from name block that accepts one contact as input and reports that person’s initials.
initials from name (name from contact( (item (1) of (contact list)) reporting 'BA'

Betsy: I used the split by block to break up the contact’s full name into a list of their names, and then I joined the first letter of each name:
initials from contact: (contact) { script variables (list of names) set (list of names) to (split (name from contact (contact)) by (word)) report (join (letter (1) of (item (1) of (list of names))) (letter (1) of (item (2) of (list of names))) ) }

What’s the difference between splitting by spaces and splitting by words?

Try it! (Each brown dot represents a space.)
split "hello␠␠␠␠␠␠␠world" by (␠) split "hello␠␠␠␠␠␠␠world" by word

In one case, split by is focusing on the exact characters in the string. In the other case, it’s giving you what you really want: the words in the contact’s name. At a low level of abstraction, text is made of characters and you look through each of the characters. But at a higher level of abstraction (such as when you are reading), text is made up of words. Snap! lets you think at this higher level of abstraction.

Alphie: But what about someone who goes by three names like Martin Luther King? It would just report “ML” for him.

Gamal: Oh, that’s because the block only takes the first letter of items 1 and 2. We have to add in letter (1) of (item (3) of (list of names)).

Betsy: But what if someone has more names than that? In my aunt’s family they each have five or six names…

Gamal: Well, then we need to take letter of for each item in the list of names. That sounds as if what we need is a higher-order function. Higher-order functions are good at doing things with individual items in a list.

Higher-Order Function

A higher-order function is a function that takes a function as input (or reports a function as output).

Higher-order blocks

The three main higher-order block you’ll need to use are the same ones you used in the Survey project and are summarized in the table and examples below:

map over list

Makes a new list with the same number of items as the original but containing new elements produced by passing each element of the original list to the ringed reporter.

picture of map transforming each item

keep items from list

Makes a new list with potentially fewer elements than the original but all the elements are from the original list. It keeps only those elements that satisfy the ringed predicate.

picture of keep blocking some input items so they don't appear in the result

combine list using

Makes a single value out of all the elements of the list by repeatedly combining elements of the list with the ringed two argument reporter.

picture of combine superimposing items into one icon

Some actual examples

map (( )-(1)) over (list (96) (-100) (4.7)) reporting

keep-items-such-that-(last-letter-of-(input)-equals-h)-from-test-list

combine (list {0, 1, 2}) with (()+()) reporting 3

  1. If it isn’t open already, open your U3L2-ContactList project.

  2. Use map over list and combine list using together to build an initials from name reporter that takes a name as input and reports that person’s initials.
    initials from name (name from contact( (item (1) of (contact list)) reporting 'BA' initials from name (Martin Luther King) reporting 'MLK'

  3. Test and debug your initials from name block.

  4. Create an expression to report a list of the initials of all your contacts.

    Need a hint?

    First, create an expression to report a list of the names of all your contacts.
    contact list watcher in list view → list in list view containing three items: Betsy Anderson, Alphie Preston, Gamal Abdel

    contact list watcher in list view → list in list view containing three items: BA, AP, GA

  5. Checkpoint

If There Is Time…
  1. Load the Assignment project which contains the following data:

    Data from Structure and Interpretation of Computer Programs by Abelson and Sussman. Creative Commons licensed.

    list block containing nine instances of the constructor 'employee, name: () job title: () salary: ()' each with various inputs

    This is the list of employees of a small company. Each of the smaller lists contains a person’s name, job title, and yearly salary.

    “This company spends more money on the big bosses than on the people who do the work,” says Alyssa one day. Is she right? Write an expression to compute the total salaries of everyone paid less than $100,000 per year. Then find the total for everyone paid more than $100,000 per year.

  2. Ben suggests that the results will be more convincing in the form of a list containing that total and all the names of the people in that category (paid less than $100,000, for example). So, if there are five people in that category, your list will have six items: first the total of the salaries, and then the names of the people. Try this both for less than $100,000 and for more than $100,000.

  3. Find the average salary of people paid less than $100,000 per year.