Difference between revisions of "W1349 Higher Order Functions"
From Coder Merlin
Line 55: | Line 55: | ||
let f = {(accumulator:Double, currentElement:Int) -> Double in | let f = {(accumulator:Double, currentElement:Int) -> Double in | ||
return accumulator - Double(currentElement)} // | return accumulator - Double(currentElement)} // | ||
let y = x.reduce(0.0, f) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Which of the following produce identical results?<br/> | Which of the following produce identical results?<br/> | ||
Line 63: | Line 63: | ||
let f = {(accumulator:Double, currentElement:Int) -> Double in | let f = {(accumulator:Double, currentElement:Int) -> Double in | ||
return accumulator + Double(-currentElement)} // | return accumulator + Double(-currentElement)} // | ||
let y = x.reduce(0.0, f) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
'''B''' | '''B''' | ||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
let x = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37] | let x = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37] | ||
let y = x.reduce(0.0, {$0 - Double($1)}) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
'''C''' | '''C''' | ||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
let x = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37] | let x = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37] | ||
let y = x.reduce(0.0) {$0 - Double($1)} // | |||
</syntaxhighlight> | </syntaxhighlight> | ||
'''D''' | '''D''' | ||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
let x = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37] | let x = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37] | ||
let y = x.reduce(0.0) {Double(-$1) + $0} // | |||
</syntaxhighlight> | </syntaxhighlight> | ||
|type="[]"} | |type="[]"} |
Revision as of 15:47, 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.