On this page, you will use higher-order functions to perform various operations on student grades: averaging, scaling, and selecting students within certain grade ranges.
set
its value to a list
of various student grades.Keep
Predicates are reporters that always report a Boolean value (they always report either or
). Predicates are the condition used by conditionals (such as
or
) to decide when to do something. In snap, predicates are hexagonal in shape, not oval like other reporters.
The block takes a predicate (with a blank input slot) and a list as input, and it creates a new list keeping only those items from the input list that match the condition defined by the predicate. 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
to report only the student grades that are over 85.scores above
reporter block that accepts a number and a list of scores as input and reports the scores that are over the inputed number. or
block together with keep
to report the student grades that are either over 90 or under 70.Map
The block takes two inputs: a function (a reporter with a blank input slot) and a list, and it reports a new list. In this new list, each item is the result of running an item from the original input list through the function. For example:
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 changed by the function.
map
block to find the list of adjusted grades.scale by
reporter block that adds the input number to every item in a list.Combine
The block takes an operation (with two blank input slots) and a list as input, and it reports a single result (not a list): the combination of the elements in the list when using the given operation. For example:
You choose the operation (with two input slots), and combine
performs that operation combining all the items in the input list and reports the result.
combine
block to help you build a block that will find the average of the student scores? What other blocks will you need?length of
list block which can be found in the Variables palette (not the green length of
operator block).average
block in Snap! that will accept a list as input and output the average of the items in the list. Try it out with your student grades list.and
block together with average
and keep
to report the student grades that are within 5 points of the class average.For a somewhat more challenging introduction to higher-order functions in a game context, see this page.