CS 252
Homework 2
Part 1. For this assignment, you will use Bill Venners' ScalaTest to produce test cases for your solution to homework 1. Here is an example, a test class to check the freeVariables function. Most of you passed that. I give you a starting point for another test to check the renameAvoiding function. I added more test cases to my version that many of you failed.
I had to go back to Scala 2.7.6 to get ScalaTest to work. Bill said he'd try to get it working with the latest Scala 2.8.0, but it may be a few more days before he'll find the time. (Everyone's hw1 code compiled under 2.7.6.)
UPDATE: Bill just posted a fix that works under 2.8.0. See here. I was able to build it by running
svn checkout https://scalatest.dev.java.net/svn/scalatest/branches/app/trunk-for-scala-2.8f --username guest cd trunk-for-scala-2.8f ant mkdir ~/scalatest-1.0 cp target/dist/lib/scalatest.jar ~/scalatest-1.0
Try this first. The Scala plugin for 2.8 is a lot less flaky than the one for 2.7. (Let me know if you need directions for installing ant.)
Deliverables:
RenameAvoidingTest.scala with more test cases. Due for first
submission.SimplifyOnceTest.scala with good coverage of
simplifyOncelambda.scala that passes your own test cases. If
it also passes mine and gives the right results on fac, powtwo and
firstmatches, I will regrade your homework #1 with it.
Draft due for first submission.test method uses closures. I want to know what
parameters test consumes, which of them are closures, and when
they are executed. Part 2. One advantage of closures is that they allow you to build new
control structures. One example is forEachProperty in Lab 8 part
3. Implement this in Scala.
Deliverables:
props.scala with an object Props
containing a forEachProperty function and a simple (not
ScalaTest) test in main, printing all system properties.Part 3. This program demonstrates how to animate the insertion sort algorithm. Note that the program has to deal with two challenges of Swing programming:
sort method starts a new
thread for animating the algorithmsetProperty method uses
EventQueue.invokeLater to make a Runnable that
Swing will execute on the event dispatch thread. Your task is to improve the program in two ways.
a) Eliminate the unsightly anonymous Runnable objects by
providing an implicit conversion from ()=>Unit to a
Runnable, so that, for example, the sort method can
be simplified to
def sort(e : ActionEvent) {
val t = new Thread(() => {
val a = randomNumberArray(10, 100)
insertionSort(a)
})
t.start()
}
Due for first submission.
b) Write a function
doTogether(tasks : Iterable[()=>Unit])
that executes all tasks in parallel, using
Executors.newCachedThreadPool.invokeAll. I could not find a way of
converting a Scala Iterable into a
java.util.Collection and had to copy the tasks into a Java
ArrayList, as I converted them to callables (using
Executors.callable).
Demonstrate this function by running the move(next, 0, 1) tasks
in parallel, so that the entire slice from j + 1 to
i moves together.
At first, I had an inelegant solution where I just added the tasks in the
same loop that shifted the array, and it looked quite un-functional. Don't do
that. Instead, use slice and map, passing the result
to doTogether.
An attempt for doTogether is due for the first submission.
Deliverables:
animate.scala, modified as described, with a function
Animation.doTogether that I can test separately