ST EK List:
No EK's found

Transforming Every List Item

While working on the SIGCSE paper, Paul and I agreed that this is still a complicated way to introduce map (namely, in the context of ADTs in a project that's already known to be difficult). I think we need a simpler introduction--perhaps in the Sentence Builder content that will be added to the Gossip lab (e.g., plurals using map). (If we do this, the reference on 3.2.2 needs an edit.) --MF, 8/31/18

On this page, you will use your ADTs with the map block to display all the names in your contact list.

As you know, the keep block allows you to check every item in a list using a predicate, and it reports only the items that make the predicate true. The map() over () block also lets you work across a whole list at once. Map allows you to perform the same function on every item of a list. Map takes two inputs: a function (a reporter with a blank input slot) and a list, and it reports a new list. Every item in this new list is the result of calling the function with an item from the original list as input. For example:
map (join( )(s)) over (list {cat, dog, bird}) reporting {cats, dogs, birds}
map (round ( )) over (list{3.14159, 87, 5.4}) reporting {3, 87, 5}

You write the function that describes the change, and map applies that function to each item in the input list and then reports the list of values reported by the function.

Notice that the function mapped over the list always has a blank input slot. With both map and keep that blank is required. This blank is where the list item goes each time the function is performed.

  1. If it isn't open already, open your U3L1-ContactList project.
  2. Import Tools Like the keep block, map isn't installed automatically. It has already been added to this project, but in other projects, you'll need to select "Import tools" from the File menu to access the map block.
    Talk with Your Partner Experiment with these examples of map. Discuss and then explain in writing what these expressions are doing.
    map (join( )(s)) over (list{block, script, Boolean})
    map ((3) * ( )) over (list{7, 8, 1})
    map (letter (1) of ( )) over (list{bounce, join, clear})
  3. Use the map block together with your selectors to report a list of only the names of all contacts.

Self-Check: Map Block

  1. This question refers to these two lists:
    set (words and numbers) to {rabbit, 5, benefit, is, 34, kite, 2, 305, the, 61}
    set (capitals) to {{Augusta, Maine}, {Boise, Idaho}, {Columbia, South Carolina}, {Des Moines, Iowa}}
    Which of these statements are true?
    Choose all that apply.

    map (letter (1) of ()) over (words and numbers) reports the list {r, 5, b, i, 3, k, 2, 3, t, 6}.
    map (item (1) of ()) over (words and numbers) reports the list {rabbit}.
    map (item (1) of ()) over (capitals) reports the list {Augusta, Boise, Columbia, Des Moines}.
    map (letter (1) of ()) over (capitals) reports the list {A, B, C, D}.
    map (all but first of ()) over (capitals) reports the list {Maine, Idaho, South Carolina, Iowa}.