Exercises following the material presented in Chapter 3
Briefly review the official collections overview, concentrating in particular on immutable collection classes, and also the parallel collections overview. Try some code examples in a REPL.
a. By copying the app-template
directory (or otherwise), create a new Scala SBT project. Write a function with signature
meanAndSD(x: Vector[Double]): (Double, Double)
which returns a tuple containing the sample mean and sample standard deviation of the collection of numbers.
b. When you get it working, write some tests to check it works on a few trivial examples.
c. Generalise it so that it works for any collection of Doubles
, and check that it works for parallel as well as serial collections.
d. Test your function on huge collections of random U(0,1) quantities. What should the true mean and standard devaition be? Can you detect a difference in speed between the serial and parallel versions?
e. (optional) You have probably written this code so that it computes the mean and SD using two passes over the data. Can you figure out a way to implement it using just a single pass?
f. (optional) You have probably completed task e. using a sequential fold which can not easily be parallelised. Can you make it parallelisable by replacing your fold
with aggregate
. You will have to look up how aggregate
works.
Starting from the code you wrote for interval bisection previously, make it safe by wrapping it in an Option. See the detailed instructions for further information.