Suppose we want the squares of all the items of a list of numbers. That's a straightforward map
problem:
Now suppose instead that we want to select the odd numbers from a list of numbers. It's a little tricky figuring out how to tell if a number is odd, but apart from that it's a straightforward keep
problem:
But what if we want the squares of the odd numbers? This is neither a simple map
nor a simple keep
, but combines aspects of both. And we can solve the problem by using the value reported by the keep
as the list input to map
:
Don't be confused about which inputs do and don't have rings. It's the square function and the are-you-odd? function that we use as the first input to each higher order function. But, even though keep
itself is a function, it's the list reported by keep
that we're using as the second input to map
.
Or if we wrote this code using the hyperblock feature of the times operator, it might look like:
In this section, we'll combine everything we know to make a better version of our acronym block. For example, if we provide the input "University of California Berkeley", the block will report the text UCB. Or if we provide the input "Beauty and Joy of Computing," the block will report the text BC. Here, our acronym block will keep the first letter of any word that is 5 or more letters.
In addition to only keeping words of 5 or more letters, another big difference between this block and your previous acroynm block is that this block accepts a text. That is, instead of the input being a list of four text values ["University", "of", "California", "Berkeley"], the input will just be one text value that says "University of California Berkeley".
To translate from a text string to a list of words, you will need to import the sentence->list
block by importing the Words and Sentences library. To import the library, click on the file icon in the upper left hand corner of Snap!, select Libraries, and import the Words and Sentences library.
Try this: Define an acronym block that behaves as described above and as pictured below.
You may use any combination of higher order functions and hyperblocks that you'd like. If you're stuck, click this link for a pure HOF solution with no hyperblocks: /bjc-r/img/list/hof/acronym.png, or click this link for a solution which also uses hyperblocks: /bjc-r/img/list/hyperblock/acronym.png.
acronym keep only capitals
block, fill in the starter code so that instead of keeping long words, it keeps words that start with a capital letter. (Hint: Experiment with the unicode of
block.)