On this page, you will use the map
block to create an initials from name
block and then use map
together with initials from name
to find the initials of all of your contacts.
initials from name
block that accepts one contact as input and reports that person's initials.
Try it! (Each brown dot represents a space.)
In one case, split
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.
letter (1) of (item (3) of (list of names))
. letter (1) of
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.A higher-order function is a function that takes a function as input (or reports a function as output).
map
function.
The block takes two inputs: a function (a reporter with a blank input slot) and a list, and it reports a new list in which each item is the result of calling the function with an item from the original list as input. For example:
You choose the function that describes the result for one input item, and map
applies that function to each item in the input list and then reports the list of result values. If your function has item 1 of
or item 2 of
in it, you're probably trying to do map
's part of the job.
Map
is a higher-order function just like keep
and combine
. The function mapped over the list always has a blank input slot. This is where the list item goes each time the function is performed.
This picture shows how the three higher-order functions could be used:
See the higher-order function expressions with these (imaginary) shape procedures.
Here is a quick review of the higher-order functions map
, keep
, combine
.
Map
performs a function on every item of a list and reports the list of changed items.Keep
uses a predicate function (a true/false question) to check every item in a list and reports the items that make the predicate true. (You learned about keep
on Unit 2 Lab 3 Page 5: Keep
ing Items from a List.)Combine
uses a combining function (a function with two inputs) to report the results of combining all the items in a list using that function. (You learned about combine
on Unit 2 Lab 4 Page 3: More Mathematical Reporters.)map
and combine
together to build an initials from name
reporter that takes a name as input and reports that person's initials.initials from name
block.
First, create an expression to report a list of the names of all your contacts.
→
item of
expects a list as its input, but map
puts a single item from words and numbers into that slot.Map
performs the input function over each item in the input list.
The list inside a list shown above makes it clearest to you now as you answer this self-check item what the structure is, but you're right, in an actual program it would be better to use an abstract data type:
Map
performs the input function on each item of the list—not on the whole list. The expression item (1) of (capitals)
(without using map
at all) would report the list {Augusta, Maine}.map (letter (1) of (item (1) of ( ))) over (capitals)
would report the list {A, B, C, D}.map (item (2) of ()) over (capitals)
would report the list {Maine, Idaho, South Carolina, Iowa}.Map
performs the input function on each item of the list—not on the whole list. The expression all but first of (capitals)
(without using map
at all) would report the list of lists {{Boise, Idaho}, {Columbia, South Carolina}, {Des Moines, Iowa}}.
Consider this list of squares:
Which of the following expressions will report a list?
Choose all that apply.
combine
will report a number.join
(1,4) will report 14. "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.