Difference between revisions of "W1349 Higher Order Functions"
From Coder Merlin
Line 397: | Line 397: | ||
+ B | + B | ||
+ C | + C | ||
- D | |||
{ | |||
Professor Snape received a new shipment of ingredients and is now sorting them onto three separate shelves. (It's very important that these ingredients not be too close to one another so as not to adversely affect their magical properties.) The first shelf is for ingredients containing blood, the second shelf is for ingredients containing hair, and the third shelf is for ingredients containing eyes. Professor Snape knows that one way to sort these ingredients is as follows: | |||
<syntaxhighlight lang="swift"> | |||
let ingredients = ["asian dragon hair", "beetle eye", "boomslang skin", "chizpurfle fang", "dragon blood", | |||
"eel eye", "eye of newt", "frog brain", "griffin claw", "horse hair", "nagini's venom", | |||
"re'em blood", "runespoor fang", "salamander blood", "toe of frog", "tongue of dog", | |||
"unicorn blood", "unicorn hair"] | |||
let containsBlood = {(ingredient:String) -> Bool in ingredient.contains("blood")} // | |||
let containsHair = {(ingredient:String) -> Bool in ingredient.contains("hair")} // | |||
let containsEye = {(ingredient:String) -> Bool in ingredient.contains("eye")} // | |||
var bloodIngredients = ingredients.filter(containsBlood) | |||
var hairIngredients = ingredients.filter(containsHair) | |||
var eyeIngredients = ingredients.filter(containsEye) | |||
</syntaxhighlight> | |||
Which of the following produce identical results?<br/> | |||
'''A''' | |||
<syntaxhighlight lang="swift"> | |||
var bloodIngredients = ingredients.filter {$0.contains("blood")} // | |||
var hairIngredients = ingredients.filter {$0.contains("hair")} // | |||
var eyeIngredients = ingredients.filter {$0.contains("eye")} // | |||
</syntaxhighlight> | |||
'''B''' | |||
<syntaxhighlight lang="swift"> | |||
var bloodIngredients = ingredients.map {$0.contains("blood")} // | |||
var hairIngredients = ingredients.map {$0.contains("hair")} // | |||
var eyeIngredients = ingredients.map {$0.contains("eye")} // | |||
</syntaxhighlight> | |||
'''C''' | |||
<syntaxhighlight lang="swift"> | |||
var bloodIngredients = ingredients.reduce {$0.contains("blood")} // | |||
var hairIngredients = ingredients.reduce {$0.contains("hair")} // | |||
var eyeIngredients = ingredients.reduce {$0.contains("eye")} // | |||
</syntaxhighlight> | |||
'''D''' | |||
<syntaxhighlight lang="swift"> | |||
var bloodIngredients = ingredients.filter {$1.contains("blood")} // | |||
var hairIngredients = ingredients.filter {$1.contains("hair")} // | |||
var eyeIngredients = ingredients.filter {$1.contains("eye")} // | |||
</syntaxhighlight> | |||
|type="[]"} | |||
+ A | |||
- B | |||
- C | |||
- D | - D | ||
</quiz> | </quiz> |
Revision as of 15:04, 24 March 2019
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Exercises[edit]
Professor Snape needs your help to organize ingredients for a wide variety of potions. You'll need to verify blocks of code to ensure that they're doing exactly what Professor Snape needs. Be careful! Any mistakes can be hazardous to his health. In some cases, more than one answer will be correct.