In Scala I need some classes to implement a dictionary. Or basically a set of ke
ID: 3594633 • Letter: I
Question
In Scala I need some classes to implement a dictionary. Or basically a set of key/value pairs where the keys are Scala strings and the associated values are abitrary scala objects. It is possible for a key to be associated with multiple values in the dictionary.
abstract class Dictionary {
/* put(k,v) adds value v to the dictionary under key k.
put may be called with the same key multiple times,
all key->values pairs get stored in the dictionary
*/
def put(key:String, value: Any):Unit
/* get(k) returns Some(v) where v is one of the values
associated with key k in the dictionary, if any.
It returns None if k is associated to no value.
*/
def get(key:String):Option[Any]
/* remove(k) removes one value v associated with key k
from the dictionary, if any, and returns it as Some(v).
It returns None if k is associated to no value.
*/
def remove(key:String):Option[Any]
/* toList() returns a list containing all the key/value pairs
in the dictionary.
If multiple values have the same key k, each is listed
in a separate pair with k.
*/
def toList():List[(String,Any)]
/* toString() returns a string containing all the key/value pairs
in the dictionary. The string has the form
Dictionary(key_1 -> value_1, ..., key_n -> value_n)
where each key_i is a key and each value_i is (the string
representation of) the corresponding value.
If multiple values have the same key k, each is present
in a separate pair with k.
*/
override def toString():String
/* getAll(k) returns the list of all the values associated
with key k in the dictionary.
*/
def getAll(key:String):List[Any] =
//replace with your implementation
/* removeAll(k) removes from the dictionary all the values
associated with key k in the dictionary, if any.
*/
def removeAll(key:String) {
// replace with your implementation
}
}
class ListDictionary extends Dictionary {
/* the dictionary is implemented using a list of key/value pairs */
private var d = List[(String,Any)]()
//implementation of Dictionary's abstract methods here
}
Explanation / Answer
Code:
import org.scalacheck._
import org.scalacheck.Arbitrary._
import org.scalacheck.Prop._
import org.scalacheck.Pretty._
trait HMap[TypedKey[_]]
{
self =>
def get[T](key: TypedKey[T]) : Option[T]
def put[T](key: TypedKey[T], value: T) : HMap[TypedKey]
}
object HMap
{
private class WrappedMap[TypedKey[_]](m: Map[TypedKey[_], AnyRef]) extends HMap[TypedKey]
{
def get[T](key: TypedKey[T]) = m.get(key).asInstanceOf[Option[T]]
def put[T](key: TypedKey[T], value: T) = new WrappedMap(m + (key -> value.asInstanceOf[AnyRef]))
}
def empty[TypedKey[_]] : HMap[TypedKey] = new WrappedMap[TypedKey](Map())
}
object ScalaCheckInstances
{
implicit def arbWithPhantom[T: Arbitrary, Phantom: Manifest] : Arbitrary[WithPhantom[T, Phantom]] = Arbitrary(for(v <- arbitrary[T]) yield WithPhantom[T, Phantom](v))
def genHMap[Value1, Value2, TypedKey[_]](implicit arbV1: Arbitrary[Value1], arbV2: Arbitrary[Value2], arbK1: Arbitrary[TypedKey[Value1]], arbK2: Arbitrary[TypedKey[Value2]]) : Gen[HMap[TypedKey]] = {
for
{
kv1List <- arbitrary[List[(TypedKey[Value1], Value1)]]
kv2List <- arbitrary[List[(TypedKey[Value2], Value2)]]
}
var hmap = HMap.empty[TypedKey]
for ((k, v) <- kv1List) { hmap = hmap.put(k, v) }
for ((k, v) <- kv2List) { hmap = hmap.put(k, v) }
hmap
}
}
implicit def arbHMap[TypedKey[_]](implicit arbKInt: Arbitrary[TypedKey[Int]], arbKString: Arbitrary[TypedKey[String]]) = Arbitrary(genHMap[Int, String, TypedKey])
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.