Combine-ing Data

Pamela, should we teach/use the distinct block? It's currently in the XML (fully built), but not discussed or used anywhere. --MF, 12/10/21
I don't feel a strong need to teach/use it. -PF
Next revision: decide whether to remove it. --MF, 12/27/21

Consider splitting this page into multiple pages. --MF, 1/11/23

In this activity, you'll create blocks to make Snap! do math with your data for you.

Snap! has another built-in block that is useful for data analysis: The combine 'list input slot' using 'predicate input slot' block combines the elements of a list using an operation. For example, this expression adds all the values in the list 0, 1, 2.
combine (list {0, 1, 2}) with (()+()) reporting 3

Notice that the function used to combine the list items needs has two blank input slots. The keep block only needs one blank in its input function, but combine requires two slots since it combines an item with the item after it.

Unlike map and keep which also take functions as inputs, the combine function is used with only a few input functions.

Which functions?

Combine is mostly used only with these built-in functions:
+ ×
and or
join join words
() max () () min ()

Why only these functions?

Combine takes a list of values as input. It combines the first two values using the input function, then combines that result with the third value using the function, then combines that result with the next value, and so on.

So, it can't matter to the input function in what order you group the items. Notice that (4 + 1) + 3 is the same as 4 + (1 + 3), but (4 − 1) − 3 is different from 4 − (1 − 3). With combine, we don't use functions where order matters (such as subtraction or division).

Finding a Maximum or Minimum Value

  1. Open your "Data Dos" project if it isn't open already.
  2. Locate the () max () and () min () blocks hidden behind the +, , ×, and / functions.
    To access them, right click the block, select "relabel...", and choose the function you want. Click for a demonstration.
    animation showing the + block, its right-click menu open with 'relabel...' selected, then another menu open showing additional operator reporter blocks with MAX selected, and finally just the MAX block in place of the + block. The animation cycles through selecting a variety of different  operator reporter blocks.
  3. Use these blocks with combine to complete the block definitions for maximum of 'list input slot' and minimum of 'list input slot'. These new blocks should take a list as input and report the maximum or minimum value in the list.
    maximum of list {1, 22, 3, -4} reporting 22 minimum of list {1, 22, 3, -4} reporting -4
  4. Test your maximum of list and minimum of list blocks by dragging a list () block into the input slot, giving it additional input spaces by clicking the right-facing triangle, and adding some numbers. Make sure both blocks work correctly before moving on.
  5. Design and answer a question to ask about your data that requires the either maximum of list or minimum of list block to answer it.
    For example, you might ask: "What's the maximum number of years that any of the people who responded to my survey have played their favorite sport?"

Finding a Sum

  1. Complete the block definition for the sum of list block so that it reports the sum of the items of a list.
    sum of {1, 2, 3, 10} reporting 16
  2. Test the sum of list block with a list of numbers before moving on.
  3. Revisit your investigation topic, and if it makes sense, design and answer a question that requires the sum block to answer it.
    For example, you might ask: "How many years in total have the people who responded to my survey played their favorite sport?"

Finding an Average

  1. Talk with Your Partner How do you find the average of a list of numbers?
    Click for a hint.

    You can find the average of a set of numbers by dividing the sum of the values by the number of values.

  2. Complete the block definition for the average of list block so that it calculates the average of the values in a list:
    average of {1, 2, 3, 4} reporting 2.5

    Click for a hint.

    Look through the palette on the left of the Snap! window. What blocks could help you do that in Snap!?

    Click for another hint.

    You'll need the length and / (division) blocks as well as the sum of list block you just finished building.

  3. Test your code for the average of list block, and fix any problems.
  4. Notice that if you hadn't checked that your sum of list block works first, then you wouldn't know whether any problems with your average of list block were really problems with average of list or were actually problems with sum of list.
  5. Now Is a Good Time to Save Design and answer a question to ask about your data that requires the average of list block to answer it.
    For example, you might ask: "How many hours on average do the people who responded to my survey listen to music each day?"

In this activity, you used the higher-order function combine to create maximum of list, minimum of list, sum of list, and average of list blocks and used them to explore your data.