Join Lists

Due: Wednesday, Oct 12 11:59PM

The type List[A] only allows sequential access to elements. For example, you can’t get to the third element until you’ve been through the first two. Instead, if you could recur on sub-lists that were significantly smaller and run each recursive call on on a different processor core, you could obtain significant speedup. This requires a different data structure, which is what we’ll explore in this assignment. Remarkably, this new data structure, will support the same operations as a list.

Preliminaries

You should create a series of directories that look like this:

 ./joinlists
 |-- build.sbt
 `-- project
     `-- plugins.sbt
 `-- src
     |-- main
     |   `-- scala
     |       `-- your solution goes here
     `-- test
        `|-- scala
             `-- your tests goes here

Your build.sbt file must have exactly these lines:

name := "joinlists"

scalaVersion := "2.11.2"

libraryDependencies += "edu.umass.cs" %% "cmpsci220" % "1.1"

libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.1" % "test"

The plugins.sbt file must have exactly this line:

addSbtPlugin("edu.umass.cs" % "cmpsci220" % "2.2")

When we grade your work, we will delete your build.sbt file and use the one above. So, if you change it, you risk getting a zero.

Introduction to Join Lists

You’ll be working with the JoinList[A] type, which is part of the cmpsci220.hw.joinlists package. It has three constructors:

It should be clear that it is very cheap to append two join lists: you simply use the Join constructor. It is also cheap to calculate the length of a join list, since it is stored at each non-trivial node of the tree.

The package includes two simple, helper functions:

Finally, since join lists representat for lists, the package has two functions to convert between join lists and lists:

Both toList and joinList are provided for testing only. You must not use them in your solution. If you do, you risk getting a zero.

Programming Task

In the file src/main/scala/Solution.scala, you must define an object called Solution that implements the JoinListFunctions trait. You can use the this template:

// src/main/scala/Solution.scala
import cmpsci220.hw.joinlists._

object Solution extends JoinListFunctions {

  def max[A](lst: JoinList[A], compare: (A, A) => Boolean): Option[A] = {
    throw new UnsupportedOperationException("not implemented")
  }

  def map[A,B](f: A => B, lst: JoinList[A]): JoinList[B] = {
    throw new UnsupportedOperationException("not implemented")
  }

  def filter[A](pred: A => Boolean, lst: JoinList[A]): JoinList[A] = {
    throw new UnsupportedOperationException("not implemented")
  }

  def first[A](lst: JoinList[A]): Option[A] = {
    throw new UnsupportedOperationException("not implemented")
  }

  def rest[A](lst: JoinList[A]): Option[JoinList[A]] = {
    throw new UnsupportedOperationException("not implemented")
  }

  def nth[A](lst: JoinList[A], n: Int): Option[A] =  {
    throw new UnsupportedOperationException("not implemented")
  }

}

Here are the specifications of these functions:

Check your work

Here is a trivial test suite that simply checks to see if you’ve defined the Solution object with the right type:

class TrivialTestSuite extends org.scalatest.FunSuite {

  test("The solution object must be defined") {
    val obj : cmpsci220.hw.joinlists.JoinListFunctions = Solution
  }
}

You should place this test suite in src/test/scala/TrivialTestSuite.scala. If this test suite does not run as-is, you risk getting a zero.

Submit Your Work

Use the submit command within sbt to create submission.tar.gz. Upload this file to Moodle.